Question

In: Computer Science

Name the script thirsty.sh. Ask the user if they are thirsty. Read the user's response. If...

  1. Name the script thirsty.sh.
  2. Ask the user if they are thirsty.
  3. Read the user's response.
  4. If they answer no or No, print an appropriate message and exit.
  5. If they answer yes or Yes, ask what they would like to drink. Read the user's response.
    • If they answer water print "Clear crisp and refreshing."
    • If they answer beer print "Let me see some id."
    • If they answer wine print "one box or two."
    • If they answer anything else print "Coming right up."

Beginner's Linux.....for loops, while loops....if/elif/el...commands...simple but clean....please help with comments so I can understand

Solutions

Expert Solution

I have executed this in Linux Ubuntu.

To execute this script, I saved it as thirsty.sh. Then we make this executable with the command: chmod +x thirsty.sh

To run this, we use : ./thirsty.sh

Since you wanted a basic script, I executed this using only if, elif and else

If, else, elif statement syntax:

if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi

The if...elif...fi statement is the one level advance form of control statement that allows Shell to make correct decision out of several conditions.

#!/bin/sh

n2="No" # storing value of No in n2
n3="no" # storing value of no in n3
n4="Yes" # storing value of Yes in n4
n5="yes" # storing value of yes in n5
water="water" # # storing value of water in water
beer="beer" # storing value of beer in beer
wine="wine" # storing value of wine in wwine
something=" " # if user enter something else, it checks here
echo "Are you thirsty" # echo to print statement
read n1 #reading the user response and storing in n1


if [ "$n1" = "$n2" ] || [ "$n1" = "$n3" ] # if statement to compare two values
then # we use || (pipe) also know as OR which is used to compare two values
   echo "You're not thirsty. We'll exit now"


elif [ "$n1" = "$n4" ] || [ "$n1" = "$n5" ] # elif statement to compare two values
then
   read -p "What would you like to drink: " d1 #storing in d1

 

if [ "$d1" = "$water" ] #  comparing d1 with water
then
    echo "Clear crisp and refreshing" # if true, this is printed
elif [ "$d1" = "$beer" ] # comparing d1 with beer
then
    echo "Let me see some id" #if true, this is printed

elif [ "$d1" = "$wine" ] comparing d1 with wine
then
    echo "one box or two"
else # if nothing is true, else is executed
    
    echo "Coming right up." # executes when if is not executed
    break # breaks the statement and the script closes

fi # closing if statement with fi
fi # closing if statement with fi

Output:

All the given conditions were verified

If you have any doubts, leave a comment below before rating. I'll be happy to assist you further.

Do UPVOTE this as I have put a lot of EFFORT in answering this question. It really helps me.


Related Solutions

Task 2.5: Write a script that will ask the user for to input a file name...
Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created 1. Open a new file script creafile.sh using vi editor # vi creafile.sh 2. Type the following lines #!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created” 3. Add the execute permission 4. Run the script #./creafile.sh 5. Enter...
Name the script while.sh. Ask the user to enter a positive integer. You can assume that...
Name the script while.sh. Ask the user to enter a positive integer. You can assume that the user will enter a positive integer (input validation for a positive number not required). Use a while loop to print all integers from 0 up to and including the integer entered. Each integer should be printed on a line by itself and nothing else should print. This is for Linux. I have not done anything on this script. Please help with simplistic commands...
C++ Read first a user's given name followed by the user's age from standard input. Then...
C++ Read first a user's given name followed by the user's age from standard input. Then use an ofstream object named outdata (which you must declare) to write this information separated by a space into a file called outdata. Assume that this is the extent of the output that this program will do. Declare any variables that you need.
Write a Python program that has the user enter their name.   Using the user's name calculate...
Write a Python program that has the user enter their name.   Using the user's name calculate the following: 1. How many letters are in the user's name? 2. Print the user's name in REVERSE both in capital letters and lowercase letters 3. Print the ASCII value for each letter of the user's name. 4. Print the SUM of all of the letters of the user's name (add each letter from #3)
C++ : Write a program that creates a login name for a user, given the user's...
C++ : Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the...
Modify the Assignment program from Module 1 to read a user's first and last name read...
Modify the Assignment program from Module 1 to read a user's first and last name read three integer scores, calculate the total and average determine the letter grade based on the following criteria - Average 90-100 grade is A, 80-89.99 grade is B, 70-79.99 grade is C, all others F (use a nested if) Output formatted results consisting of the full name, the three scores, the total, average (to 2 decimal places), and the letter grade go to step 1...
In Kali Linux Write a script that ask the user to enter an IP address and...
In Kali Linux Write a script that ask the user to enter an IP address and a port number, then use the provided entries from the user to perform a query on your local network and determine if the given port is open on the provide network. Need to submit solutions for both below. 1.A short report showing the functionality of your code 2. your bash script
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
Your Application should ask the user to enter their name and their salary.
Create a C# Console Application, name the solution Homework 6 and the project TaxRate.Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric...
Write a bash script that will allow you to: Read in your Your name Course name...
Write a bash script that will allow you to: Read in your Your name Course name and Instructor’s name Then prompt the user to enter two numbers and determine which number is greater. Ex: If 10 is entered for the first number and 3 for the second, you should output Your name Course name Instructor’s name 10 is greater than 3. If 33 is entered for the first number and 100 for the second, you shou output Your name Course...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT