Question

Please write a BASH Script in LINUX that... 1. is actually executable 2. has a comment...

Please write a BASH Script in LINUX that...

1. is actually executable
2. has a comment to tell us what you did, why and how.
3. allows a user to enter their name and a number between 1 than 100 (this must be prompted so the user knows what to do)
4. creates a random number between 1 and 100 for you to guess. The command to create a random number is shown below. (if you find a better one...use it...)
5. checks to see if an improper number is entered. If they do they will get an error and exit. You do not have to test if a "non-number" has been entered.
6. continues to let you guess until you get the answer correct.
7. tells you "higher or lower" and asks you for another guess until you get it right.
8. banners "CORRECT YAY" and exits if you get it in 2 guesses or less.
9. says "yes the answer was, . It took you tries" and exits if it takes you more than 2 tries.
10. adds an entry to the file /var/tmp/guessme.txt with the name of the user, the date (from the "date" command) and the number of tries each time through keeping a history of the game.

HELPFUL INFO:

first make sure you have everything you need. run the following command...
echo 1+1 | bc
...if you don't get "2", but instead get an error, perform the following command as root
yum install bc -y

next make sure you have the banner command...run
banner hello world
...if you get an error instead of a set of large letters, please execute the following command as root...
yum install banner -y


The command to generate a random number into a variable called "GUESSME" is...
let GUESSME=`echo $NUMINPUT*$RANDOM/32767 | bc -l | awk -F'.' '{print $1}'`+1
...this assumes that there is a variable already called "NUMINPUT" that was read in from the script.
RANDOM is a local variable that constantly changes. Its range is from 0-32767...try it yourself...run..."echo $RANDOM" a whole bunch of times.

you'll need the read command...here's an example... read -p "Enter a Number " ANUMBER ... This will ask you to "Enter a Number" on the command line and then put that number in a variable called "ANUMBER"

you'll need an "if" statement to check to see if the number is OK...if it's not OK you will nee the "exit" command to break out of the script.

you will need a while loop to continue guessing until you are right... once you are right you will need the "break" command so the loop does not continue (or maybe you don't....) for an example of a while loop, look at the other the file "bond.txt" which we covered in class (it is also listed in the collection of files in your "Files" tab)

you will need to keep tract of the number of your guesses so incrementing a counter is done with the "let" command for instance "let COUNTER=$COUNTER+1"

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The script guess.sh is written below. Following this, I have pasted the IDE screenshot as well for syntax highlighting and indentation. The script has inline comments wherever required to understand the logic.

#!/bin/bash

# Read in two values from the user input
# Name in $FULLNAME and a number in $NUMINPUT
read -p "Enter your name please: " FULLNAME
read -p "Enter a number between 1 and 100 " NUMINPUT

# Basic validation for input. If the number is <1 or
# >100, it's an invalid input, the program will quit
if [ $NUMINPUT -lt 1 ] || [ $NUMINPUT -gt 100 ]; then
        echo "Invalid input, try again"
        exit 2
fi

# Generate a random number as given in the ask
# mark this number in the variable $GUESSME
let GUESSME=`echo $NUMINPUT*$RANDOM/32767 | bc -l | awk -F'.' '{print $1}'`+1

#echo $GUESSME       # For debugging purposes, uncomment this line

# Need to keep track of a counter to see how many tries the user took
let COUNTER=0

# Main loop that we need to run forever until the user guesses the right number
while :
do
        # Test if the last number was matching the Random number or not
        # in case it matched, and the $COUNTER is <= 2, then it means
        # in 2 or lesser attempts, user guessed the number right.
        if [ $GUESSME -eq $NUMINPUT ] && [ $COUNTER -le 2 ]; then

                # Print a banner, please note that since I am using Ubuntu
                # banner package is not available, hence I am using an
                # alternative of banner, that is figlet.
                figlet "CORRECT YAY!"   # Replace figlet with banner

                # Write the history of name, date and number of attempts in the
                # /var/tmp/guessme.txt. Since date is a command, it had to be
                # enclosed under backticks (``).
                echo $FULLNAME ----- `date` ----- $COUNTER >> /var/tmp/guessme.txt
                exit 1
        elif [ $GUESSME -eq $NUMINPUT ] && [ $COUNTER -gt 2 ]; then
                # At this level, the user's guess was correct but he/she took more
                # than 2 attempts, print the appropriate message and write its
                # history in the guessme.txt file again
                # >> is for appending the echo output in the file
                echo "yes, the answer was," $NUMINPUT ". It took you tries."
                echo $FULLNAME ----- `date` ----- $COUNTER >> /var/tmp/guessme.txt
                exit 1
        fi

        # if the random number is < the input number, ask to guess again
        # with a lesser number and update the variable NUMINPUT with
        # new user input number
        if [ $GUESSME -lt $NUMINPUT ]; then
                read -p "Guess with a lesser number " NUMINPUT
        # vice versa of if-block
        elif [ $GUESSME -gt $NUMINPUT ]; then
                read -p "Guess with a greater number " NUMINPUT
        fi

        # if the control reaches here, that means the number has not been guessed
        # yet. So, we increase the count of tries by 1 and reiterate the loop.
        let COUNTER=$COUNTER+1
done

IDE screenshot:

Making the script executable and executing it:

Add a comment
Know the answer?
Add Answer to:
Please write a BASH Script in LINUX that... 1. is actually executable 2. has a comment...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a....

    Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a. Write the following as script6.sh and change its permissions to count for num in d do if num eq 100 then count (count+1) done echo The number 10 was found $count times What is the output? Run the script as ./script6.sh 10 20 30 10 40 20 50 10 60 <enter Summarize what the script does r using the read command. The following script...

  • newd in C++ Exercise #3 Guess the Number Write a "Guess the Number" game that randomly...

    newd in C++ Exercise #3 Guess the Number Write a "Guess the Number" game that randomly assigns a secret number [1,20] for the user to guess. I recommend hard coding your secret number at first to make testing your code easier. You can write this a number of ways, but try to stick to the instructions outlined here. Write three methods to make your game work. The first method should generate a random number and return the value to main...

  • I posted this earlier but the answer did not provide three methods. Can you please post...

    I posted this earlier but the answer did not provide three methods. Can you please post the answer with three methods. Also if possible can it be in C#. Java is also fine but please have three methods in the code. Also the main method. Exercise #3 Guess the Number Write a "Guess the Number" game that randomly assigns a secret number [1,20] for the user to guess. I recommend hard coding your secret number at first to make testing...

  • I need help with a C++ assignment: Write a program containing the following: 1. Variable Definitions...

    I need help with a C++ assignment: Write a program containing the following: 1. Variable Definitions only as (DieRoll, Guess, cnt1, cnt2) followed by this statement: srand((unsigned int)time (NULL)); which will give the random number generator a random starting point. Note: srand and rand require the TIME.H (or iomanip) cnt1 and cnt2 will be used in Chapter 5 drop box as counters for loops. Do NOT create additional variables. Points will be taken off for any additional variable creation. 2....

  • Problem 1 Write a BASH script to create a user account from the Linux system on...

    Problem 1 Write a BASH script to create a user account from the Linux system on which your script is run. The script should process two positional parameters. First positional parameter is supposed to be a string with a user name (e.g., user_name) Second positional parameter is supposed to be a string with a user password (e.g., user_password) In your script: Check if two positional parameters were passed to your script when it was invoked If NOT, print an appropriate...

  • Write a Bash script called move that could replace the UNIX command mv. 'move' tries to...

    Write a Bash script called move that could replace the UNIX command mv. 'move' tries to rename the source file (using the UNIX command mv), but if the destination file exists, appends an index number, a sort of version number, to the destination file. So if the user types: move a.txt b.txt and b.txt already exists, move will rename the file to b.txt.1. If b.txt.1 already exists, move must rename the file to be b.txt.2, and so on, until the...

  • In c# create a program that generates a random number from 1 to 1000. Then, ask...

    In c# create a program that generates a random number from 1 to 1000. Then, ask the user to guess the random number. If the user's guess is too high, then the program should print "Too High, Try again". If the user's guess is too low, then the program should print "Too low, Try again". Use a loop to allow the user to keep entering guesses until they guess the random number correctly. Once they figure it, congratulate the user....

  • Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts...

    Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts the user for a guess. Use the pseudorandom number generator, the Random class. When the user guesses the correct number, say so, and display how many guesses the user required to guess correctly. Allow the user to play multiple times by asking the user if the user wants to continue playing.      Guess my number between 1 and 1000 => 500      Too high...

  • Please write a bash script for the following two questions, thank you.    Write a shell script...

    Please write a bash script for the following two questions, thank you.    Write a shell script “6-1.sh” to calculate the exponentiation using while loop. This script needs to take two inputs like “6-1.sh a b”, and outputs the result of “ab”. Note: You need to implement it, DO NOT use the exponentiation operation provided by the Linux.    Write a shell script “6-2.sh” to calculate the factorial of a number using while loop. This script needs to take one input...

  • Hello Please, don't answer with your handwriting, Thank you.. This question is related to java language...

    Hello Please, don't answer with your handwriting, Thank you.. This question is related to java language Write a full Java program that: 1. Generate an integer random number between 0 and 6. 2. Then make a loop that keeps asking the user to guess the generated random number. If the user input is not equal to the generated random number, then print, "Try again" and allow the user to input another guess until the user guess correctly 3. If the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT