-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestionLoop.sh
More file actions
40 lines (39 loc) · 1.07 KB
/
questionLoop.sh
File metadata and controls
40 lines (39 loc) · 1.07 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
# loop until question answer is = to string in array
#
function askQuestionLoop(){
# required
if [ -z $2 ]; then
echo ;
echo '---- arguments missing ---';
echo 'askQuestionLoop <<question>> <<name of answers array>>';
echo ;
echo 'example script:';
echo ;
echo "# array of allowed answers";
echo "allowed=('y' 'n')";
echo "# run script";
echo 'askQuestionLoop "my question" allowed ';
echo '# use answer';
echo 'echo "returned = $returned";'
return;
fi;
#
local i; # make not usable outside function
local varName; # make not usable outside function
local question=${1};
#
local name=$2[@]; # name of the array
local allowedOptions=("${!name}"); # the array content
#
local loop=true;
while [ "$loop" == true ]; do
echo question;
read varName;
for i in "${allowedOptions[@]}"; do
if [ "$i" == "$varName" ] ; then
returned="$varName";
loop=false;
fi
done
done
}