-
Notifications
You must be signed in to change notification settings - Fork 0
/
case_switch.sh
47 lines (38 loc) · 1.08 KB
/
case_switch.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
answers=("a" "d" "c" "a" "a")
total_score=0
for (( i = 0; i < 5; i++));
do
case "${1}" in # switch(${1})
${answers[i]}) # case answers[i]:
((total_score++)) # total_score++
;; # break
"-"|"--")
;;
*) # default:
((total_score--))
;;
esac
# shifts script argument to the left removing left most argument
# this way we can use ${1} for every argument as it's shifted on every iteration of the loop
shift 1
done
echo "Total score is: $total_score"
# case aka switch. Two options conditions with the use of 'or' '|'
case "${1}" in
"")
echo "No option was selected."
;;
1 | 'a')
echo "You either selected '1' or 'a'."
;;
2 | 'b')
echo "You either selected '2' or 'b'."
;;
3 | 'c')
echo "You either selected '3' or 'c'."
;;
*)
echo "Unknown character '${1}'."
;;
esac