Creating a Bash Script Menu

Hi guys!
Id love some help in trying to build a bash script menu.
What Im trying to do is the following...

1. Run a bash script eg.menu.sh
2. When run, it presents the following information......

===============================
--------- BASH MENU: Menu.sh ---------
===============================
Execute the following commands by typing the correct number...

[1] Connect to fire's temp folder
[2] Connect to fire's backup folder
[3] Switch to fire's temp folder
[4} Switch to fire's backup folder
[5] Disconnect from fire's backup folder
[6] Disconnect from fire's temp folder

-----------------------------------------------------------------------------------------
3. The user types '1' to connect ot fire's temp folder for example.


4. The commands I have for this are:

----------------------------------------------
[1] Connect to fire's temp folder
mount -t cifs //192.168.2.3/fire_temp /mnt/net_temp_h_fire_fire_temp -v -o user=scripter,pass=password,domain=WORKGROUP,rw'

----------------------------------------------
[2] Connect to fire's backup folder
'mount -t cifs //192.168.2.3/fire_backups /mnt/net_backups_h_fire_fire_backups -v -o user=scripter,pass=password,domain=WORKGROUP,rw'

----------------------------------------------
[3] Switch to fire's temp folder
cd /mnt/net_temp_h_fire_fire_temp; clear; echo "===== h:\fire\fire_temp ====="; pwd; ls -ltra | more'

----------------------------------------------
[4} Switch to fire's backup folder
cd /mnt/net_backups_h_fire_fire_backups; clear; echo "===== h:\fire\fire_backups ====="; pwd; ls -ltra | more'

----------------------------------------------
[5] Disconnect from fire's backup folder
umount /mnt/net_backups_h_fire_fire_backups

----------------------------------------------
[6] Disconnect from fire's temp folder
umount /mnt/net_temp_h_fire_fire_temp

================
I will add more above, but i just want to get the general idea how you would build this menu.sh script, so that a menu is presented, and all you have to do is press a number for the option you want to be executed.
Any ideas greatly appreciated.



LVL 1
Simon336697Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcuk_stormCommented:
Attached is the code to achieve what you asked, it will continually go back to the menu after an option is choosen and executed, until 7(exit) is selected.

Kind Regards,

mcuk_storm
#!/bin/bash
 
function outputMenu {
cat > /dev/stdout <<DELIM
===============================
--------- BASH MENU: Menu.sh ---------
===============================
Execute the following commands by typing the correct number...
 
[1] Connect to fire's temp folder
[2] Connect to fire's backup folder
[3] Switch to fire's temp folder
[4} Switch to fire's backup folder
[5] Disconnect from fire's backup folder
[6] Disconnect from fire's temp folder
[7] Exit Menu
DELIM
 
read USERCHOICE
clear
 
echo Executing your selected option...
echo
 
if [ $USERCHOICE -eq 1 ]; then
mount -t cifs //192.168.2.3/fire_temp /mnt/net_temp_h_fire_fire_temp -v -o user=scripter,pass=password,domain=WORKGROUP,rw
 
elif [ $USERCHOICE -eq 2 ]; then
mount -t cifs //192.168.2.3/fire_backups /mnt/net_backups_h_fire_fire_backups -v -o user=scripter,pass=password,domain=WORKGROUP,rw
 
elif [ $USERCHOICE -eq 3 ]; then
cd /mnt/net_temp_h_fire_fire_temp; clear; echo "===== h:\fire\fire_temp ====="; pwd; ls -ltra | more
 
elif [ $USERCHOICE -eq 4 ]; then
cd /mnt/net_backups_h_fire_fire_backups; clear; echo "===== h:\fire\fire_backups ====="; pwd; ls -ltra | more
 
elif [ $USERCHOICE -eq 5 ]; then
umount /mnt/net_backups_h_fire_fire_backups
 
elif [ $USERCHOICE -eq 6 ]; then
umount /mnt/net_temp_h_fire_fire_temp
 
elif [ $USERCHOICE -eq 7 ]; then
	exit 0
 
else
	echo Unknown Option $USERCHOICE
fi
 
}
 
 
until [ "1" = "0" ]; do
	clear
 
	outputMenu
 
	echo Press enter to continue...
	read
done

Open in new window

0
mcuk_stormCommented:
A slight revision to that, so that it treats the user input as a string rather than an integer, as you never know what they may type in.


#!/bin/bash
 
function outputMenu {
cat > /dev/stdout <<DELIM
===============================
--------- BASH MENU: Menu.sh ---------
===============================
Execute the following commands by typing the correct number...
 
[1] Connect to fire's temp folder
[2] Connect to fire's backup folder
[3] Switch to fire's temp folder
[4} Switch to fire's backup folder
[5] Disconnect from fire's backup folder
[6] Disconnect from fire's temp folder
[7] Exit Menu
DELIM
 
read USERCHOICE
clear
 
echo Executing your selected option...
echo
 
if [ "$USERCHOICE" = "1" ]; then
mount -t cifs //192.168.2.3/fire_temp /mnt/net_temp_h_fire_fire_temp -v -o user=scripter,pass=password,domain=WORKGROUP,rw
 
elif [ "$USERCHOICE" = "2" ]; then
mount -t cifs //192.168.2.3/fire_backups /mnt/net_backups_h_fire_fire_backups -v -o user=scripter,pass=password,domain=WORKGROUP,rw
 
elif [ "$USERCHOICE" = "3" ]; then
cd /mnt/net_temp_h_fire_fire_temp; clear; echo "===== h:\fire\fire_temp ====="; pwd; ls -ltra | more
 
elif [ "$USERCHOICE" = "4" ]; then
cd /mnt/net_backups_h_fire_fire_backups; clear; echo "===== h:\fire\fire_backups ====="; pwd; ls -ltra | more
 
elif [ "$USERCHOICE" = "5" ]; then
umount /mnt/net_backups_h_fire_fire_backups
 
elif [ "$USERCHOICE" = "6" ]; then
umount /mnt/net_temp_h_fire_fire_temp
 
elif [ "$USERCHOICE" = "7" ]; then
	exit 0
 
else
	echo Unknown Option $USERCHOICE
fi
 
}
 
 
until [ "1" = "0" ]; do
	clear
 
	outputMenu
 
	echo Press enter to continue...
	read
done

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pétur Ingi EgilssonSoftware Engineer -- ConsultantCommented:
hi

I'd rather use "case" than "if" in this approach.-, it makes the code much much more readable

Here's my modified version of mcuk_storm's answer:
 #!/bin/bash
 
function outputMenu {
cat > /dev/stdout <<DELIM
===============================
--------- BASH MENU: Menu.sh ---------
===============================
Execute the following commands by typing the correct number...
 
[1] Connect to fire's temp folder
[2] Connect to fire's backup folder
[3] Switch to fire's temp folder
[4} Switch to fire's backup folder
[5] Disconnect from fire's backup folder
[6] Disconnect from fire's temp folder
[7] Exit Menu
DELIM
 
read USERCHOICE
clear
 
echo Executing your selected option...
echo
 
case $USERCHOICE in
        "1") mount -t cifs //192.168.2.3/fire_temp /mnt/net_temp_h_fire_fire_temp -v -o user=scripter,pass=password,domain=WORKGROUP,rw;;
        "2") mount -t cifs //192.168.2.3/fire_backups /mnt/net_backups_h_fire_fire_backups -v -o user=scripter,pass=password,domain=WORKGROUP;;
        "3") cd /mnt/net_temp_h_fire_fire_temp; clear;
                echo "===== h:\fire\fire_temp =====";
                pwd; ls -ltra | more;;
        "4")  cd /mnt/net_backups_h_fire_fire_backups;
                clear;
                echo "===== h:\fire\fire_backups =====";
                pwd; ls -ltra | more;;
        "5") umount /mnt/net_backups_h_fire_fire_backups;;
        "6") umount /mnt/net_temp_h_fire_fire_temp;;
        "7") exit 0;;
        *) echo Unknown Option $USERCHOICE;;
esac
}
 
until [ "1" = "0" ]; do
        clear
 
        outputMenu
 
        echo Press enter to continue...
        read
done

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Simon336697Author Commented:
You guys are INCREDIBLE and INCREDIBLY HELPFUL AND KIND!!!

I will test and report back to both of you :>)
0
Simon336697Author Commented:
They are both brilliant!

Guys I cant thank you enough.

Guys they work great. Can I just finish off by asking you both...

In the case example, what does the
esac
at the end of the case actually do?

What does the

until [ "1" = "0" ]; do

until 1 actually do?

You guys are great.
0
mcuk_stormCommented:
The until loop basically just loops forever, it will loop until 1 = 0 which will never happen.

In the case, esac marks the end of the case statement (the } at the end it a bit miss leading you may be mistaken for thinking that was the end of the case but it is the end of the function), so esac just says there are no more cases to be defined for that construct.

mcuk_storm
0
Simon336697Author Commented:
mcuk_storm!

Mate thanks so much that explains it thanks again :>)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.