-
Notifications
You must be signed in to change notification settings - Fork 0
/
guessinggame.sh
46 lines (46 loc) · 1.29 KB
/
guessinggame.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
#calculate the actual number of files in the current directory
actual_numb_files=$(ls -A | wc -l) #taking almos all
#actual_numb_files=$(ls -al | grep '^-' | wc -l) #old approach
#this function compares the given response to actual number of files
#it returns a string that gives the actual results
function compare_guess {
local delta #function returns this variable
if [[ $response -gt $actual_numb_files ]]
then
delta="too high"
elif [[ $response -lt $actual_numb_files ]]
then
delta="too low"
else
delta="correct"
fi
echo $delta
}
#initialize the loop
keep="Y"
while [[ "$keep" == "Y" ]]
do
echo "Guess the number of files in the current directory."
checknumber="KO"
while [[ "$checknumber" == "KO" ]] #check input
do
echo "Give a non negative number:"
read response
if ! [[ $response =~ ^[0-9]+$ ]] #check format
then
echo "The input, $response, is not a non negative number. Try again"
checknumber="KO"
else
checknumber="OK"
fi
done
result=$(compare_guess)
echo "You entered: $response, that is $result." #verify whether keep or not
if [[ "$result" == "correct" ]]
then
echo "Congratulations, you guessed correctly!"
keep="N"
else
echo "Please keep guessing..." #read keep
fi
done