Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a script in Bourne shell that presents a user with a menu as follows: A

ID: 3910855 • Letter: 1

Question

1. Write a script in Bourne shell that presents a user with a menu as follows:

Add the numbers (enter a or A)

Subtract the numbers (enter s or S)

Multiply the numbers (enter m or M)

Divide the numbers (enter d or D)

Exit (select e or E)

If the user enters any other character other than the ones mentioned, then print an error saying it is not a choice and ask the user to select again until the correct option is chosen.

Once the operation is done, ask the user if they want to do another operation on the same two integers or do another operation with two different integers or exit. Do accordingly based on the users choice. So until the user does not select the exit option, the user is presented with a menu. Also, when you run the script, provide two integers as command line arguments. The beginning of your script should have an if statement that checks how many arguments are coming in and present a few statements about Usage of the script if the number of arguments is not 2.

2. Write a script in Bourne shell that asks the user for an integer between 1 and 10 and prints out sum of integers from 1 through the number entered and also the product of the integers from 1 through the number entered (which is also called the factorial).

So for example, if the user enters 4, then the script should print the sum as 10 (1+2+3+4) and 24 (1*2*3*4).

Explanation / Answer

Script for answer 1:

#!/bin/bash

if [ $# != 2 ]; then

echo "Usage $0 <num1> <num2>"

exit

fi

var1=$1

var2=$2

echo "entered variables: $var1, $var2"

function readNextInput()

{

echo "do you want another operation on same integers(p) or want to input another pair of integers(q)? "

read -p "Enter your choice[p/q] " op1

case $op1 in

q) read -p "input integers separated by spaces: " var1 var2

echo "new variables: $var1 $var2";;

?) continue;;

esac

}

while true

do

read -p "pls enter your choice: A - Add, S - Subtract, M - Multiply, D - Divide, E - Exit: " op

case $op in

A) ;&

a) echo "$var1 + $var2 = $(($var1 + $var2))"

readNextInput;;

S) ;&

s) echo "$var1 - $var2 = $(($var1 - $var2))"

readNextInput;;

M) ;&

m) echo "$var1 * $var2 = $(($var1 * $var2))"

readNextInput;;

D) ;&

d) echo "$var1 / $var2 = $(($var1 / $var2))"

readNextInput;;

E) ;&

e) echo "exiting..."

exit;;

?) echo "invalid choice. enter A/S/M/D"

esac

done

Sample Output:

vagrant@ubuntu-xenial:~/chegg$ ./menu.sh 5 5

entered variables: 5, 5

pls enter your choice: A - Add, S - Subtract, M - Multiply, D - Divide, E - Exit: D

5 / 5 = 1

do you want another operation on same integers(p) or want to input another pair of integers(q)?

Enter your choice[p/q] q

input integers separated by spaces: 6 2

new variables: 6 2

pls enter your choice: A - Add, S - Subtract, M - Multiply, D - Divide, E - Exit: S

6 - 2 = 4

do you want another operation on same integers(p) or want to input another pair of integers(q)?

Enter your choice[p/q] p

pls enter your choice: A - Add, S - Subtract, M - Multiply, D - Divide, E - Exit: e

exiting...

Script for answer 2:

#!/bin/bash

read -p "please enter a number between 1 to 10: " num

echo "The number entered is " $num

if !([ "$num" -gt 1 ] && [ "$num" -le 10 ]); then

   echo "input number not between 1 and 10. Exiting.."

   exit

fi

f=1

for i in $(seq $num)

do

   f=$(($f * $i))

done

echo "factorial : $f"

s=0

for i in $(seq $num)

do

   s=$(($s + $i))

done

echo "sum: $s"


Sample Output:

vagrant@ubuntu-xenial:~/chegg$ ./number.sh

please enter a number between 1 to 10: 5

The number entered is 5

factorial : 120

sum: 15