summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2020-10-30 11:49:17 -0400
committerAndrew Cady <d@cryptonomic.net>2020-10-30 12:10:14 -0400
commit2b2060df3cc0460128774b2c167c06f562cf77e2 (patch)
tree9a040613cf781a20d711f5f0dd4f3072f68c7814
parent05303b249d858e48715cc0e1debbb94827cceca3 (diff)
usb.sh can choose between multiple devices
-rwxr-xr-xsrc/usb.sh28
1 files changed, 24 insertions, 4 deletions
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 @@
2 2
3die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; } 3die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; }
4 4
5ECHO() { printf "%s\n" "$*" >&2; }
6
7PRINTF() { printf "$@" >&2; }
8
5whole_device() 9whole_device()
6{ 10{
7 case "$1" in 11 case "$1" in
@@ -16,7 +20,7 @@ whole_device()
16confirm_usb() 20confirm_usb()
17{ 21{
18 local msg="This will completely overwrite device:\n\n\t%s\n\nType 'yes' to confirm.\nContinue? " 22 local msg="This will completely overwrite device:\n\n\t%s\n\nType 'yes' to confirm.\nContinue? "
19 printf "$msg" "$1" >&2 23 PRINTF "$msg" "$1"
20 read line 24 read line
21 case "$line" in 25 case "$line" in
22 [yY][eE][sS]) return ;; 26 [yY][eE][sS]) return ;;
@@ -37,9 +41,25 @@ choose_usb()
37 die "no usb device found" 41 die "no usb device found"
38 elif [ $# = 1 ]; then 42 elif [ $# = 1 ]; then
39 confirm_usb "$1" || die impossible 43 confirm_usb "$1" || die impossible
40 USB=$(readlink -f "$1") 44 USB=$1
41 else 45 else
42 die "multiple USB devices connected and choice between them is unimplemented. ($*)" 46 ECHO
47 PRINTF "%s\n" "The following candidate target devices have been found:"
48 i=0
49 for dev; do
50 i=$((i+1))
51 PRINTF "\n\n\t%2i. %s\n" $i "$dev"
52 done
53 PRINTF "%s\n\n" '' "Choose a device by its number, from 1 to $#, then press enter." "To abort, press CTRL-C."
54 PRINTF "\n%s" 'Your choice: '
55 read line
56 case "$line" in
57 [1-9]|[1-9][0-9])
58 [ $line -le $# -a $line -ge 1 ] || die "Invalid argument (out of range): $line"
59 USB=${!line}
60 ;;
61 *) die "Invalid argument: $line" ;;
62 esac
43 fi 63 fi
44} 64}
45 65
@@ -49,5 +69,5 @@ choose_cdrom()
49} 69}
50 70
51choose_usb 71choose_usb
52echo $USB 72[ "$USB" ] && readlink -f "$USB"
53 73