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.
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
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
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
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)
I installed CentOS 8 with Gnome 3.28.2 and I noticed that the "switch user" menu item disappeared from under the system menu of Gnome classic (Both X11 & Wayland). I checked google and this problem seems to have a history going back several releases of Gnome.
Unfortunately, I never found a... (1 Reply)
hi all i am a newbie to this
is there any examples on creating a main menu with 3 sub menu
main menu -> option a , b and c
a menu -> option 1 ,2 and 3
b menu -> option 1 ,2
c menu -> option 1 ,2
i am getting headache as my code kept getting unexpected EOF
---------- Post... (0 Replies)
Hi!
I have created on script which is working fine with menu options and with a sub-menu.
I want to enhance it by using sub-options under menu options.
Like.
option 1) will give the list only.
option 1.1) should give the option to user to choose one file, whose content user wanna see. ... (3 Replies)
Hi,
I need to have a shell script for the below need.
1. Menu with one heading and 4 options.
2. the heading and 4 options are taken from a file.
File entry
======
Heading1|option1|option2|option3|option4|
Heading2|option1|option2|option3|option4|
3. the user entries must be captured in... (9 Replies)
The code im having problems with is highlighted in red, upon selecting option 2 on the main menu (highlighted green) my echo "NETWORK CONNECTIVITY" command seems to get overlooked and the resulting output is "Thank you for using the Operator Administrative Tool." being displayed. Can anyone tell me... (2 Replies)
So, I need help writing a Menu script in UNIX.
First how can I use letters for the options instead of numbers?
Also, is there any templates out on the web I can use? (2 Replies)
I'm working on a script for a basic menu with four options. The first option for the menu asks a user to put in anyone's user name and is supposed to display the user's home directory. If a user does not enter a name, then their own home directory should be displayed. The second menu option is... (0 Replies)
It's me again :)
I would like to create a script that will display a 'Main Menu' which will link to other scripts in the same directory. Is it possible to have a scrolling menu and pretty colours for this? :) (9 Replies)