Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

create a bash script that generates a random number using the shell\'s built-in

ID: 639138 • Letter: C

Question

create a bash script that generates a random number using the shell's built-in RANDOM variable. The user will be asked to enter a number between 1 and 10, inclusive. The loop will continue until the user guesses the correct answer. The $RANDOM statement will generate a random number between 0 and 32,767; therefore a $RANDOM statement within the loop will ensure a random number between 1 and 10, inclusive, is generated by the system. 1. In a terminal window, create a bash shell script named hw1 in the $HOME/bin directory. 2. Use vi or your favorite editor. Let me get you started: After the shebang, type:

read -p "Guess a number between 1 and 10: " guess

rand=$RANDOM

while true

do

if [[ $rand -ge 1 && $rand -le 10 ]]

then if [[ $guess -eq $rand ]]

blah, blah, blah

fi

fi

done

3. Save the script. Make it executable. Run it, fix it, run it again [repeat].

4. Now that it runs, add code to display the number of times it took the player to guess correctly.

Explanation / Answer

If you already have your random number, you can say

to get numbers from 0..12.

Edit: If you want to produce numbers from $x to $y, you can easily modify this: