From 2b2060df3cc0460128774b2c167c06f562cf77e2 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Fri, 30 Oct 2020 11:49:17 -0400 Subject: usb.sh can choose between multiple devices --- src/usb.sh | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/usb.sh b/src/usb.sh index 55d8b48..991f553 100755 --- a/src/usb.sh +++ b/src/usb.sh @@ -2,6 +2,10 @@ die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; } +ECHO() { printf "%s\n" "$*" >&2; } + +PRINTF() { printf "$@" >&2; } + whole_device() { case "$1" in @@ -16,7 +20,7 @@ whole_device() confirm_usb() { local msg="This will completely overwrite device:\n\n\t%s\n\nType 'yes' to confirm.\nContinue? " - printf "$msg" "$1" >&2 + PRINTF "$msg" "$1" read line case "$line" in [yY][eE][sS]) return ;; @@ -37,9 +41,25 @@ choose_usb() die "no usb device found" elif [ $# = 1 ]; then confirm_usb "$1" || die impossible - USB=$(readlink -f "$1") + USB=$1 else - die "multiple USB devices connected and choice between them is unimplemented. ($*)" + ECHO + PRINTF "%s\n" "The following candidate target devices have been found:" + i=0 + for dev; do + i=$((i+1)) + PRINTF "\n\n\t%2i. %s\n" $i "$dev" + done + PRINTF "%s\n\n" '' "Choose a device by its number, from 1 to $#, then press enter." "To abort, press CTRL-C." + PRINTF "\n%s" 'Your choice: ' + read line + case "$line" in + [1-9]|[1-9][0-9]) + [ $line -le $# -a $line -ge 1 ] || die "Invalid argument (out of range): $line" + USB=${!line} + ;; + *) die "Invalid argument: $line" ;; + esac fi } @@ -49,5 +69,5 @@ choose_cdrom() } choose_usb -echo $USB +[ "$USB" ] && readlink -f "$USB" -- cgit v1.2.3