Skip to content

Instantly share code, notes, and snippets.

@kotobukid
Created September 9, 2025 06:05
Show Gist options
  • Select an option

  • Save kotobukid/ed52bf59698afbb442131deb63dba819 to your computer and use it in GitHub Desktop.

Select an option

Save kotobukid/ed52bf59698afbb442131deb63dba819 to your computer and use it in GitHub Desktop.
指定の名前でダウンロードしたファイルをmicro:bitにコピーするだけ
#!/bin/sh
# Watch a file's mtime and copy it to ~/MICROBIT on change.
# Usage: watch_and_copy.sh -f <file>
set -u
TARGET_DIR="$HOME/MICROBIT"
usage() {
echo "Usage: $0 -f <file>" 1>&2
}
FILE=""
while [ "$#" -gt 0 ]; do
case "$1" in
-f)
shift
[ "$#" -gt 0 ] || { echo "-f requires a file path" 1>&2; usage; exit 2; }
FILE="$1"
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1" 1>&2
usage
exit 2
;;
esac
shift
done
if [ -z "${FILE}" ]; then
echo "Missing required -f <file> option" 1>&2
usage
exit 2
fi
if [ ! -e "${FILE}" ]; then
echo "File not found: ${FILE}" 1>&2
exit 1
fi
# Try to resolve MICROBIT mount dynamically at copy time
resolve_target_dir() {
# 1) Explicit env override
if [ "${MICROBIT_DIR-}" != "" ] && [ -d "${MICROBIT_DIR}" ]; then
echo "${MICROBIT_DIR}"
return 0
fi
# 2) User home fallback
if [ -d "${TARGET_DIR}" ]; then
echo "${TARGET_DIR}"
return 0
fi
# 3) Common Linux mount points
if [ "${USER-}" != "" ]; then
if [ -d "/media/$USER/MICROBIT" ]; then
echo "/media/$USER/MICROBIT"
return 0
fi
if [ -d "/run/media/$USER/MICROBIT" ]; then
echo "/run/media/$USER/MICROBIT"
return 0
fi
for d in /media/"$USER"/* /run/media/"$USER"/*; do
[ -d "$d" ] || continue
base=`basename "$d"`
if [ "$base" = "MICROBIT" ]; then
echo "$d"
return 0
fi
done
fi
# 4) macOS
if [ -d "/Volumes/MICROBIT" ]; then
echo "/Volumes/MICROBIT"
return 0
fi
# 5) /proc/mounts scan as a last resort
if [ -r /proc/mounts ]; then
mp=`awk '$2 ~ /MICROBIT/ {print $2; exit}' /proc/mounts 2>/dev/null`
if [ "${mp}" != "" ] && [ -d "${mp}" ]; then
echo "${mp}"
return 0
fi
fi
echo ""
return 1
}
# Detect how to read mtime on this system.
MTIME_IMPL=""
if stat -c %Y "${FILE}" >/dev/null 2>&1; then
MTIME_IMPL="gnu"
elif stat -f %m "${FILE}" >/dev/null 2>&1; then
MTIME_IMPL="bsd"
elif date -r "${FILE}" +%s >/dev/null 2>&1; then
MTIME_IMPL="gnu_date"
elif ls -ld --time-style=+%s "${FILE}" >/dev/null 2>&1; then
MTIME_IMPL="ls"
else
echo "Unable to determine file mtime on this system" 1>&2
exit 1
fi
get_mtime() {
case "$MTIME_IMPL" in
gnu)
stat -c %Y "$1" 2>/dev/null
;;
bsd)
stat -f %m "$1" 2>/dev/null
;;
gnu_date)
date -r "$1" +%s 2>/dev/null
;;
ls)
ls -ld --time-style=+%s "$1" 2>/dev/null | awk '{print $6}'
;;
*)
return 1
;;
esac
}
LAST_MTIME="$(get_mtime "${FILE}")"
if [ -z "${LAST_MTIME}" ]; then
echo "Failed to read mtime for ${FILE}" 1>&2
exit 1
fi
cleanup() {
echo "SIGTERM受信: 終了します" 1>&2
exit 0
}
trap cleanup TERM
while :; do
# If the file disappeared, skip this cycle quietly
if [ ! -e "${FILE}" ]; then
sleep 2
continue
fi
CURRENT_MTIME="$(get_mtime "${FILE}")"
if [ -n "${CURRENT_MTIME}" ] && [ "${CURRENT_MTIME}" != "${LAST_MTIME}" ]; then
# Resolve destination dynamically each attempt
DEST_DIR=`resolve_target_dir`
if [ "${DEST_DIR}" = "" ]; then
echo "MICROBITのマウント先が見つかりません。接続/マウントを待機中…" 1>&2
else
DEST_PATH="${DEST_DIR}/$(basename "${FILE}")"
cp -v -- "${FILE}" "${DEST_PATH}"
if [ $? -eq 0 ]; then
LAST_MTIME="${CURRENT_MTIME}"
fi
fi
fi
sleep 2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment