LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   creating submenus in bash (http://www.linuxquestions.org/questions/programming-9/creating-submenus-in-bash-4175443511/)

eamesj 12-30-2012 05:51 PM

creating submenus in bash
 
Hi all,

Trying to build a series of options in a bash script and im fine with 1 menu but i want to put a sub menu in one of the options...

#!/bin/bash
menu ()
{
clear
echo "-------------------------------"
echo " M A I N - M E N U"
echo " 1) option "
echo " 2) option "
echo " 3) option "
echo " 4) option "
echo "-------------------------------"
while
do
read action
case $action in
"1")
do something
;;
"2")
do something else
;;
"3")
submenu ()
{
clear
echo "-------------------------------"
echo "SUB -MENU"
echo "a) option"
echo "b) option"
echo "-------------------------------"
while :
do
read subaction
case $subaction in
"a")
do something ;;
"b")
do something else ;;
esac
done
}
submenu;
;;
"4")
echo "Bye!"
exit 0
;;
*)
echo "Error: Invalid option..."
read -p "Press [Enter] key to continue..." readEnterKey
;;
esac
done
}
menu;


but bash is complaining that there's a syntax error, can someone tell me how can i create in submenus in bash ?

thanks,

eamesj 12-30-2012 06:34 PM

solved
 
Solved it

Hadnt added a ;; what i wrote is actually correct. Can see why its called bash...

solution:

3)
submenu ()
{
clear
echo "-------------------------------"
echo "SUB -MENU"
echo "a) option"
echo "b) option"
echo "-------------------------------"
while :
do
read subaction
case $subaction in
"a")
do something ;;
"b")
do something else ;;
esac
done
}
submenu;
;;
4)

David the H. 12-30-2012 10:28 PM

Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


I recommend not putting the submenu function definition inside the main menu function. Functions are generally intended to be defined at the top of the script, so that they will be sitting in the environment ready to use when you need them.

It also lessens the clutter, helping readability.


Edit:
Also, your first loop has an error:

Code:

while                #should be "while :"
do
read action

By the way, many experienced scripters feel that it's more readable to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as it more clearly separates the outside block from the inside block. Be sure to indent all sub-commands properly, too.

Code:

while true; do
        read action
        ...
done

I also personally prefer using the true keyword to ":" in this kind of loop.


All times are GMT -5. The time now is 04:03 AM.