Question

In: Computer Science

write a script named compute.sh that is used to do simple arithmetic for the user. There...

write a script named compute.sh that is used to do simple arithmetic for the user. There should be no command line arguments. Instead, all values from the user should be prompted for and read in to the script using the read command. Specifically, you need to ask the user for two integers and an operation string. The operation should be "add", "sub", "mul", "div", or "exp", for addition, subtraction, multiplication, division, and exponentiation, respectively. Your script should take the two numbers, perform the requested operation, and output the result. Here is a sample run:

[user@localhost ~]$ compute.sh

Enter an integer: 5

Enter another integer: 7

Enter an operation (add,sub,mul,div,exp): mul

5 * 7 = 35

[user@localhost ~]$

The script will have to translate the operation string to the correct operation using either a case statement or if/else statements. Note that you do not need to verify that the numbers given are actual numbers, but you DO need to verify that the operation string is one of the five listed operations. If it isn't one of the five, print an appropriate error message and exit.

Be sure to test your script thoroughly. When you're done, TAKE A SCREENSHOT (5) of the output of the script using some example inputs.

TAKE A SCREENSHOT (6 – computer.sh) your script. I can’t grade if your screenshot is not readable. Bigger font please! If you can’t control font size, please submit the file as text format.

Solutions

Expert Solution

echo "Enter an integer: "
read number1
echo "Enter an integer: "
read number2
echo "Enter an operation (add,sub,mul,div,exp):"
read operation

if [ "$operation" == "mul" ]
then
   result=$((number1 * number2))
   echo "$number1 * $number2 = $result"
  
elif [ "$operation" == "add" ]
then
   result=$((number1 + number2))
   echo "$number1 + $number2 = $result"
  
elif [ "$operation" == "sub" ]
then
   result=$((number1 - number2))
   echo "$number1 - $number2 = $result"
  
elif [ "$operation" == "div" ]
then
   result=$((number1 / number2))
   echo "$number1 / $number2 = $result"

elif [ "$operation" == "exp" ]
then
   result=$((number1 ** number2))
   echo "$number1 ** $number2 = $result"

else
   echo "Invalid operation"
   exit
fi



Related Solutions

SQL Homework -- This script creates the schema named mgs -- Connect as the user named...
SQL Homework -- This script creates the schema named mgs -- Connect as the user named mgs --CONNECT cs270226mgs/mgs; -- Use an anonymous PL/SQL script to -- drop all tables and sequences in the current schema and -- suppress any error messages that may displayed -- if these objects don't exist BEGIN EXECUTE IMMEDIATE 'DROP SEQUENCE category_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE product_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE customer_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE address_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE order_id_seq'; EXECUTE IMMEDIATE 'DROP...
Write a bash script that... create new user ./finalProject user if a new user is to...
Write a bash script that... create new user ./finalProject user if a new user is to be created ask for the new users information and use it when creating the new user add a new printer ./finalProject printer ask anything you need in order to create the new printer (I.e. name) permissions ./finalProject permissions ask what document and permissions the user wants restarting the computer ./finalProject restart
Write a complete shell script that first asks the user to enter a URL. The script...
Write a complete shell script that first asks the user to enter a URL. The script should read the URL into a variable named url. The shell script should then retrieve the file associated with the URL by using the curl command. The output of the curl command should be redirected to a file named html_file. The shell script should then use the grep command to search the file named html_file for the word manhattan. Finally, the shell script should...
Write a script named numberlines.py. This script creates a program listing from a source program. This...
Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the user for the names of two files. The input filename could be the name of the script itself, but be careful to use a different output filename! The script copies the lines of text from the input file to the output file, numbering each line as it goes. The line numbers should be right-justified in 4 columns, so that the...
3. a) Write a script to prompt the user for the length and width of a...
3. a) Write a script to prompt the user for the length and width of a rectangle, and print its area with two decimal places. Put comments in the script. b) Convert the script in part (a) to a function.
Write a script that prompts the user for a pathname of a file or directory and...
Write a script that prompts the user for a pathname of a file or directory and then responds with a description of the item as a file along with what the user's permissions are with respect to it (read, write, execute).
Write a Powershell script to create a Windows user account that is a non-admin user. This...
Write a Powershell script to create a Windows user account that is a non-admin user. This assignment assumes that you already have a Windows VM, with the administrator account created.. To complete the assignment, write the PowerShell script, run it on your VM, and submit the script here.
Write a short bash script that takes in two arguments from a user, asks the user...
Write a short bash script that takes in two arguments from a user, asks the user whether they would like to add, subtract, multiply, or divide. Each of these operations must be a function that returns data.
1. Write a simple batch include IFELSE statments? 2. Write a simple Powershell Script with IFELSE...
1. Write a simple batch include IFELSE statments? 2. Write a simple Powershell Script with IFELSE statements? 3. Explain this powershell script and write its output? $x = 30 if($x -eq 10){ write-host("Value of X is 10") } elseif($x -eq 20){ write-host("Value of X is 20") } elseif($x -eq 30){ write-host("Value of X is 30") } else { write-host("This is else statement") }
I need assistance in making a simple html and php script that lets a user explor...
I need assistance in making a simple html and php script that lets a user explor the RGB color specturum. It should be able to use 6 buttons (R+,G+,B+,R-,G-,B-) that when pressed, add or subtract 2 points (ranging from 0-255) of that color from the RGB ratio. The new RGB color is then displayed in a small window/box at the bottom of the page for the user to see. This should allow the user to explore all the different colors...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT