-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYNQ.func
More file actions
52 lines (50 loc) · 1.61 KB
/
YNQ.func
File metadata and controls
52 lines (50 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- shell-script -*-
#
# dot this file into your scripts
function YNQHelp() {
shout.short "enter one of" \
"'y', 'Y' to perform the action (this is the default)" \
"'n', 'N' to skip" \
"'q', 'Q' to quit (and exit)" \
"'?' to show this message"
}
# YNQ will print the first argument as a prompt and then will repeatedly read
# the users response until it gets Y, N or Q. If it gets a response of 'Y',
# 'y' or '' then it will eval the remaining parameters to YNQ, print "Done"
# and return 0. It it gets a response of 'N' or 'n' then it will print
# "Skipping" and return 1. If it gets a response of 'Q' or 'q' then it will
# print "Quitting" and exit with status 1. If it gets a response of '?' then
# it will print a help message and reprompt. Any other response and this will
# print an error message and reprompt.
function YNQ() {
prompt=$1
shift
errorPrinted="N"
REPLY="XXX"
while [ "$REPLY" != "" -a "$REPLY" != "y" -a "$REPLY" != "Y" -a \
"$REPLY" != "n" -a "$REPLY" != "N" -a \
"$REPLY" != "q" -a "$REPLY" != "Q" ]
do
read -n 1 -p "$prompt"'? ([y]/n/q): '
if [ -n "$REPLY" ] ; then echo ; fi
if [ "$REPLY" = "q" -o "$REPLY" = "Q" ] ; then
shout.alert "Quitting"
exit 1
elif [ "$REPLY" = "n" -o "$REPLY" = "N" ] ; then
shout.short "Skipping"
return 1
elif [ "$REPLY" = "y" -o "$REPLY" = "Y" -o "$REPLY" = "" ] ; then
eval "$*"
shout.short "Done"
return 0
elif [ "$REPLY" = "?" ] ; then
YNQHelp
else
if [ "$errorPrinted" = "N" ] ; then
shout.short "bad reply: '$REPLY'"
YNQHelp
errorPrinted="Y"
fi
fi
done
}