iDdraig
November 27, 2010, 5:31pm
1
Problem:
I am trying to create a menu in a menu script and I am running into an issue with the calculator portion of the script. I am first presented with the ==Options Menu== which all 5 options working correctly. Now comes the fun part. I select option 1 which takes me to my ==Calculator Program== and then select option 1 again to run an addition calculation. After entering in my numbers and being presented with an answer, I am asked "Do you want to continue (Y/N)." If I select Y, I am present with another "Do you want to continue (Y/N)" and selecting Y again will bring me back to the ==Options Menu==. If I had selected N the first time, I would have exited the script entirely.
Intended Product
I am trying to understand how to loop the script to go back to the ==Calculator Program== menu upon selecting Y or just loop back automatically after the answer is provided and allow the user to exit by the selectable menu.
#!/bin/bash
#Menu Script
clear
BLUE="\033[0;34;48m"
RED="\033[0;31;48m"
BLACK="\033[0;38;48m"
while true; do
echo "==Option Menu=="
echo "1 - Call a calculator"
echo "2 - Display files in user directory"
echo "3 - Display the contents of the file that user enters"
echo "4 - Sort the content of file that user enters"
echo "5 - Logout"
echo "Enter your option"
read option
case $option in
1)
echo ==Calculator Program==
echo "1 - Addition"
echo "2 - Subtraction"
echo "3 - Multiplication"
echo "4 - Division"
echo "5 - Exit"
echo "Please enter your option"
read option
case $option in
1)
echo ==Addition==;
echo Enter first number;
read a
echo Enter second number;
read b
op=`expr $a + $b`
if [ $op -ge 0 ]
then
echo -e Answer is: $BLUE"$op"$BLACK
else
echo -e Answer is: $RED"$op"$BLACK
fi
;;
2)
echo ==Subtraction==;
echo Enter first number;
read a
echo Enter second number;
read b
op=`expr $a - $b`
if [ $op -ge 0 ]
then
echo -e Answer is: $BLUE"$op"$BLACK
else
echo -e Answer is: $RED"$op"$BLACK
fi
;;
3)
echo ==Multiplication==
echo Enter first number;
read a
echo Enter second number;
read b
op=`expr $a \* $b`
if [ $op -ge 0 ]
then
echo -e Answer is: $BLUE"$op"$BLACK
else
echo -e Answer is: $RED"$op"$BLACK
fi
;;
4)
echo ==Division==
echo Enter first number;
read a
echo Enter second number;
read b
op=`expr $a / $b`
if [ $op -ge 0 ]
then
echo -e Answer is: $BLUE"$op"$BLACK
else
echo -e Answer is: $RED"$op"BLACK
fi
;;
5)
echo "Ok bye";
exit 0
;;
*)
echo "Enter valid option"
;;
esac
echo "Do you want to continue (Y/N)"
read opt;
echo $opt | grep -i 'y'
if [[ $? -eq 0 ]]
then
clear
else
exit 0
fi
;;
2)
echo Sort;
;;
3)
echo Delete;
;;
4)
echo Display;
;;
5)
echo "Ok bye";
exit 0
;;
esac
echo "Do you wnat to contine (Y/N)"
read opt;
echo $opt | grep -i 'y'
if [[ $? -eq 0 ]]
then
clear
else
exit 0
fi
done
ctsgnb
November 27, 2010, 6:35pm
2
You can lop in ==Calculator Program==
just the same way you loop in ==Option menu==
You can use break
to get out of a while
loop without exiting the entire script.
Use function when you do some tasks many times.
By the way, here is a sample of your code with some little improvment
case $option in
1)
compute(){
echo $1
echo "Enter first number" ; read a
echo "Enter second number"; read b
op=`expr $a "$SIGN" $b`
[[ $op -ge 0 ]] && COLOR=$BLUE || COLOR=$RED
echo -e Answer is: $COLOR"$op"$BLACK
}
while :
do
echo ==Calculator Program==
echo "1 - Addition"
echo "2 - Subtraction"
echo "3 - Multiplication"
echo "4 - Division"
echo "5 - Back to previous Menu"
echo "6 - Exit"
echo "Please enter your option"
read option
MSG="" ; SIGN=''
case $option in
1) MSG="==Addition==" ; SIGN='+' ; compute "$MSG" ;;
2) MSG="==Subtraction==" ; SIGN='-' ; compute "$MSG" ;;
3) MSG="==Multiplication==" ; SIGN='*' ; compute "$MSG" ;;
4) MSG="==Division==" ; SIGN='/' ; compute "$MSG" ;;
5) clear ; break ;;
6) echo "Ok bye" ; exit 0 ;;
*) echo "Enter valid option" ;;
esac
done
;;
If we want to exit, there already is a choice in the menu, so .... the following lines are useless (and even bad for the workflow of query)
echo "Do you want to contine (Y/N)"
read opt;
echo $opt | grep -i 'y'
if [[ $? -eq 0 ]]
then
clear
else
exit 0
fi