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

Create a script file called mycalc using vim . The first line of your script fil

ID: 3874564 • Letter: C

Question

Create a script file called mycalc using vim.

The first line of your script file should force the use of the bash shell.

On the top of the script file, there should be a section of comments that contains the Assignment number, your name, student number, lab section number, name of the script file, the date, and a description of what the script file does.

Your script file should be properly commented

The script file should perform the following operations: + and

The script should have two functions:

add and subtract

Each of the functions should accept two integer numbers as parameters

The functions should perform the desired calculation.

The script file should support two ways of functioning, no parameters or three parameters.

If three parameters are provided, the second parameter must be + or :

The First and Third parameters must be numbers.

The user can enter integer numbers only.

The result should be displayed on the screen and the program exit.

The example below shows how plus would work. Do similar for -

Example:

mycalc 12 + 3

The sum of 12 plus 3 equals 15

If no parameters are provided the script should do the following:

A menu should be provided allowing the user to exit or Do a calculation. Allow upper or lower case. See Menu 1 below.

If C is selected the screen should clear and the user should be prompted to enter a number. See Menu 2 below.

If a number is entered the screen should clear and a new menu should be displayed. See Menu 3 below.

If + or - is entered the screen should clear and the user should be prompted to enter a number. See Menu 2 below.

If a number was entered the result should be shown on the screen

After three seconds the screen should clear and Menu 1 should be displayed again.

Error checking should be done to make sure of the following:

Either no parameters or three parameters are entered.

If three parameters are entered then the second parameter must be one of the following + -

Any invalid choices should be flagged in the menu system

You don’t need to check if the input numbers are integers

Menu 1

Menu 2

Menu3

C) Calculation

X) Exit

Please enter an integer number or X to exit:

+) Add

- ) Subtract

X) Exit

Menu 1

Menu 2

Menu3

C) Calculation

X) Exit

Please enter an integer number or X to exit:

+) Add

- ) Subtract

X) Exit

Explanation / Answer

#!/bin/bash

#function to add numbers
add_numbers()
{
num1=$1
num2=$2
sum=$((num1 + num2))
echo "sum of $num1 and $num2 is $sum"
}

#function to substract numbers
substract_numbers()
{
num1=$1
num2=$2
sum=$((num1 - num2))
echo "substraction of $num1 and $num2 is $sum"
}


#get parameters
if [ $# -eq 0 ] || [ $# -eq 3 ] ; then   
#caluclation in case of 3 parameters
if [ $# -eq 3 ]; then
if [ $2 == '+' ]; then
add_numbers $1 $3
elif [ $2 == '-' ]; then
substract_numbers $1 $3
fi
exit 0   
else
#calculation in case of 0 parameters
while :
do
#menu1
echo "C) Calculation"
echo "X) Exit"
read menu1
case $menu1 in
C)
#do calculation
clear
#menu2
echo "Please enter an integer number or X to exit:"
read menu2
case $menu2 in
X)
exit
;;
*)  
#get first number
num1=$menu2  
clear
#menu3 display
echo "+) Add"
echo "- ) Subtract"
echo "X) Exit"
  
  
read menu3
case $menu3 in
+)
clear
#menu2 display
echo "Please enter an integer number or X to exit:"
#get second number for addition
read num2
add_numbers $num1 $num2
;;
-)
clear
#menu2 display
echo "Please enter an integer number or X to exit:"
#get second number for substraction
read num2
substract_numbers $num1 $num2
;;
X)
exit
;;
esac
#sleep for 3 seconds and clear the screen
sleep 3
clear   
;;
esac
;;   
X)   
exit
break
;;
*)
echo " "
;;
esac
done
  
fi
  
else
echo "Your command line contains zero or three parameters : mycalc 12 + 13 or mycalc"
exit 0
fi

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote