-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt_yn.sh
More file actions
43 lines (41 loc) · 866 Bytes
/
prompt_yn.sh
File metadata and controls
43 lines (41 loc) · 866 Bytes
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
##
# Prompt user for a yes or no response
#
# Arguments:
# --invert Invert the response (yes becomes 0, no becomes 1)
# --default-yes Default to yes if no response is given
# --default-no Default to no if no response is given
#
# Returns:
# 1 for yes, 0 for no (or inverted if --invert is set)
#
function prompt_yn() {
local YES=1
local NO=0
local DEFAULT="n"
local PROMPT="Yes or no?"
local RESPONSE=""
while [ $# -ge 1 ]; do
case $1 in
--invert) YES=0; NO=1;;
--default-yes) DEFAULT="y";;
--default-no) DEFAULT="n";;
*) PROMPT="$1";;
esac
shift
done
echo "$PROMPT" >&2
if [ "$DEFAULT" == "y" ]; then
DEFAULT="$YES"
echo -n "> (Y/n): " >&2
else
DEFAULT="$NO"
echo -n "> (y/N): " >&2
fi
read RESPONSE
case "$RESPONSE" in
[yY]*) echo $YES ;;
[nN]*) echo $NO ;;
*) echo $DEFAULT;;
esac
}