diff options
-rwxr-xr-x | src/usb.sh | 28 |
1 files changed, 24 insertions, 4 deletions
@@ -2,6 +2,10 @@ | |||
2 | 2 | ||
3 | die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; } | 3 | die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; } |
4 | 4 | ||
5 | ECHO() { printf "%s\n" "$*" >&2; } | ||
6 | |||
7 | PRINTF() { printf "$@" >&2; } | ||
8 | |||
5 | whole_device() | 9 | whole_device() |
6 | { | 10 | { |
7 | case "$1" in | 11 | case "$1" in |
@@ -16,7 +20,7 @@ whole_device() | |||
16 | confirm_usb() | 20 | confirm_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 | ||
51 | choose_usb | 71 | choose_usb |
52 | echo $USB | 72 | [ "$USB" ] && readlink -f "$USB" |
53 | 73 | ||