-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquestion.sh
executable file
·47 lines (38 loc) · 1.24 KB
/
question.sh
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
#!/usr/bin/env bash
#------------------------------------------------------------------------------
# Arguments:
# $1 = the question
# $2 = the default answer if the user just hit return
# Return:
# User's response as string
#------------------------------------------------------------------------------
function prompt_question() {
local question="${1}"
local default_answer="${2}"
read -r -p "${question} (${default_answer}) " answer
[ -z "${answer}" ] && answer="${default_answer}"
echo "${answer}"
}
function multiple_choice() {
local question="${1}"
local csv="${2}"
local default_answer="${3}" # Needs to be an integer
local buf="${question}"
buf+=$'\n'
IFS=',' read -r -a choices <<< "${csv}"
for (( i=0; i<"${#choices[@]}"; i++ )); do
buf+="${i} - ${choices[i]}"
buf+=$'\n'
done
read -rp "${buf}Your choice (${default_answer} - ${choices[${default_answer}]}) " answer
if [ -z "${answer}" ]; then
answer="${choices[${default_answer}]}"
else
answer="${choices[${answer}]}"
fi
echo "${answer}"
}
path=$(prompt_question 'Enter path to save file?' '/home/me')
echo "User entered ${path}."
direction=$(multiple_choice 'Where do you want to go?' 'North,South,East,West' 0)
echo "User want to go ${direction}."