In: Computer Science
Create shell scripts, .sh file. Please include a shebang line at
the top of the script as well as appropriate comments through
out.
Write a script that plays a simple “guess the number” game with the
user. It should select a random integer in the range [0 - 10], ask
the user to guess the value, and provide feedback of the form “too
high”, “too low”, or “congratulations” as appropriate. After the
correct value is guessed, the script should terminate.
#!/bin/sh
#echo is use to print the statement in terminal
#read is use to take input from terminal
echo "Guess The Number: "
read ip
#this line generates random number based on second field, which is (0-29)
random=$(date | cut -d " " -f 5 | cut -d ":" -f 3)
#this line will convert the random number (0-29) to (0-10)
random=$((random%11))
while [ 1 ] #always true condition
do
if [ $ip -eq $random ] #if the user gussed number become equal to that random number
then
echo "Congratulations" #print congratulations
break #and break the loop & also terminate the script
elif [ $ip -gt $random ] #if user inputed number is greater then random number
then
echo "Too high" #then it will print Too high
else
echo "Too low" #else, means the number is smaller then random number then it will print Too low
fi
echo "Guess Again: "
read ip #and both the cases (greater than & less than) then this line will ask for again input
done