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

Write a Python script that will be used as a calculator to allow simple arithmetic computation...
Write a Python script that will be used as a calculator to allow simple arithmetic computation of two input integer numbers. Requirements: a. The script should first allow the user to select if they want to add, subtract, multiply or divide two integer digits. b. The script should pass the user inputs to a function using arguments for computation and the results return to the main part of the script. c. The return value will be tested to determine if...
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 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...
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...
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 simple PHP script. In your script use all of the following: HTML, Javascript, and...
Write a simple PHP script. In your script use all of the following: HTML, Javascript, and PHP.
This problem requires you to prompt the user for some information, and apply simple arithmetic operation...
This problem requires you to prompt the user for some information, and apply simple arithmetic operation to generate an output. We all know that driving is expensive. So let's write a program to observe this. Your job is to prompt the driver for miles per gallon and gas price (in dollars) per gallon You should prompt the user with the words: Enter miles per gallon: at which time the users enters a number, and then Enter the gas price:at which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT