In: Computer Science
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.
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