Answer:
Here is the bash shell script:
scorefile="scores_bash"
guess=-1
typeset -i n=0
echo -e "guess.bash - Guess a number between 0 to 128\\"
(( answer = RANDOM % 128 + 0 ))
while (( guess != answer )); do
n=n+1
read -p "Enter guess $n: " guess
if (( guess < answer )); then
echo "Low guesss"
elif (( guess > answer )); then
echo "High guess"
fi
done
echo -e "You guessed the number in $n guesses.\\"
read -p "Enter your name: " name
echo $n $name >> $scorefile
echo -e "\\Scores"
sort -n $scorefile
Step-by-step explanation:
The program creates a scorefile as scores_bash to store the names and scores. Next the program initializes the guess with -1. guess is used to store the guess made by user and n is used to store the number of trials user took to guess the correct number. Next the program prompts the user to: Guess a number between 0 to 128 and generates random numbers from 0 to 128 and store it into answer. Next the while loop keeps repeating until the user guesses the correct number. If the user guess is less than the correct answer then the program displays the message Low guess otherwise displays High guess. When the user guesses the correct number, the loop breaks and the message You guessed the number in $n guesses. is displayed where in place of $n the number of trials user take to guess the number is displayed. Next the user is asked to enter his name.
echo $n $name >> $scorefile statement adds the n which is the user score and name which is the names of user to the score file. sort -n $scorefile sorts the file in ascending order to display the lowest scores. The screenshot of program with its output is attached.