Friday, June 6, 2008

Bash script menu


This is how we can create a menu (continuous) in bash scripting.

If you need to write a small script to run your "tests suite", I am presenting here an efficient way(I feel so) of writing a menu.

_________
Way 1
_________


#!/bin/sh

f_Reg () {
echo "Regression function"
echo "All your logic in this function"
}

f_Smoke () {
echo "Smoke function"
echo "All your logic in this function"
}

while : # Loop forever
do
cat << !

R U N M E N U

1. Regression Tests
2. Smoke Tests
3. Quit

!

echo -n " Your choice? : "
read choice

case $choice in
1) f_Reg ;;
2) f_Smoke ;;
3) exit ;;
*) echo "\"$choice\" is not valid "; sleep 2 ;;
esac
done


Executing:


$ ./menu.sh

R U N M E N U

1. Regression Tests
2. Smoke Tests
3. Quit

Your choice? :


________
Way2:
________

The other way of making a continuous menu is to have a variable set to "no", and exit the menu when the value is "yes" (set the value to "yes" for the quit option in the menu like below):


#!/bin/sh

quit="no"

f_Reg () {
echo "Regression function"
echo "All your logic in this function"
}

f_Smoke () {
echo "Smoke function"
echo "All your logic in this function"
}

while [ $quit != "yes" ]
do
echo "1. Regression Tests"
echo "2. Smoke Tests"
echo "3. Quit"
echo -n "Your choice? : "
read choice

case $choice in
1) f_Reg ;;
2) f_Smoke ;;
3) quit="yes" ;;
*) echo "\"$choice\" is not valid"
sleep 2 ;;
esac
done


Executing:


$ ./menu1.sh
1. Regression Tests
2. Smoke Tests
3. Quit
Your choice? :

1 comment:

peej said...

Hi J,
What would you suggest for several menus showing several options (around 100 in total). I am getting bogged down by so much code and so many options. I am thinking it may be better to do in python with menu screen formats and command files but I am worried about the section of actually executing the commands. What do you think? Thanx very much. p

© Jadu Saikia www.UNIXCL.com