Write a menu driven program using FUNCTION Linux shell progr
Write a menu driven program using FUNCTION (Linux shell programming language) to test some of the arithmetic operators.
Example:
Main Menu
1--------------------------------ADD
2--------------------------------SUB
3--------------------------------MPY
4--------------------------------MOD
5--------------------------------DIV
6--------------------------------EXIT
The program must prompt user for input i.e.
Please enter two numbers to be added.
Also must print out the input and the result (output).
Solution
Answer :
#!/bin/bash
# SCRIPT: ArithematicOperatins.sh
# USAGE: ArithematicOperatins.sh
# PURPOSE: Add,Sub,Mpy,Mod,Div operations of two numbers.
clear #Clears Screen
Bold=\"\\033\" #Storing escape sequences in a variable.
Normal=\"\\033\"
echo -e \"$Bold Arithmetic Operations Using Linux Shell Programming $Normal\ \"
items=\"
1-----------ADD
2-----------SUB
3-----------MPY
4-----------MOD
5-----------DIV
6-----------EXIT\"
choice=
# simply remove functions and its entries from main script.
exit_function()
{
clear
exit
}
#Function enter is used to go back to menu and clears screen
enter()
{
unset num1 num2
ans=
echo \"\"
echo -e \"Do you want to continue(y/n):\\c\"
stty -icanon min 0 time 0
# When -icanon is set then one character has been received.
# min 0 means that read should read 0 characters.
# time 0 ensures that read is terminated the moment one character
# is hit.
while [ -z \"$ans\" ]
do
read ans
done
#The while loop ensures that so long as at least one character is not received read continue to get executed
if [ \"$ans\" = \"y\" -o \"$ans\" = \"Y\" ]
then
stty sane # Restoring terminal settings
clear
else
stty sane
exit_function
fi
}
while true
do
echo -e \"$Bold \\t Main MENU $Normal\ \"
echo -e \"\\t$items \ \"
echo -n \"Enter your choice : \"
read choice
case $choice in
1) clear
echo \"Enter two numbers for ADD : \"
echo -n \"Number1: \"
read num1
echo -n \"Number2: \"
read num2
echo \"$num1 + $num2 = `expr $num1 + $num2`\"
enter ;;
2) clear
echo \"Enter two numbers for SUB : \"
echo -n \"Number1: \"
read num1
echo -n \"Number2: \"
read num2
echo \"$num1 - $num2 = $((num1-num2))\"
enter ;;
3) clear
echo \"Enter two numbers for MPY : \"
echo -n \"Number1: \"
read num1
echo -n \"Number2: \"
read num2
echo \"$num1 * $num2 = `echo \"$num1*$num2\"|bc`\"
enter ;;
4) clear
echo \"Enter two numbers for MOD : \"
echo -n \"Number1: \"
read num1
echo -n \"Number2: \"
read num2
echo \"$num1 % $num2 = `echo \"$num1%$num2\"|bc`\"
enter ;;
5) clear
echo \"Enter two numbers for DIV : \"
echo -n \"Number1: \"
read num1
echo -n \"Number2: \"
read num2
let div=num1/num2
echo \"$num1 / $num2 = $div\"
enter ;;
6) exit_function ;;
*) echo \"You entered wrong option, Please enter 1,2,3,4,5 or 6\"
echo \"Press enter to continue\"
read
clear
esac
done
Output :
Enter two numbers for ADD :
Number1: 45
Number2:32
45 + 32 = 77
Do you want to continue(y/n) :n
........................................................


