-
Notifications
You must be signed in to change notification settings - Fork 0
/
guessinggame.sh
39 lines (33 loc) · 1023 Bytes
/
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
#!/usr/bin/env bash
# File: guessinggame.sh
#This program will continuously ask the user to guess the number of files in the current directory, until they guess the correct number.
#The user will be informed if their guess is too high or too low.
#Once the user guesses the correct number of files in the current directory they should be congratulated.
echo "Guess how many files are in the current directory?"
read response
game='on'
function how_many_files_in_here {
ls -l | grep -v ^d | wc -l
}
while [[ $game = 'on' ]]
do
if [[ $response =~ ^[0-9]+$ ]]
then
if [[ $response -eq $(how_many_files_in_here) ]]
then
echo "Hoooray - You Guessed!"
let game='off'
elif [[ $response -gt $(how_many_files_in_here) ]]
then
echo "Try again - the number you provided is too high"
read response
elif [[ $response -lt $(how_many_files_in_here) ]]
then
echo "Try again - the number you provided is too low"
read response
fi
else
echo "Please give integer number"
read response
fi
done