LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script menu with sub menu (https://www.linuxquestions.org/questions/programming-9/bash-script-menu-with-sub-menu-4175491779/)

sbrock 01-19-2014 02:47 AM

Bash script menu with sub menu
 
Hi All,

I am hoping someone is able to help me out with a script, the first component of the menu works fine with selections and goes to the sub menu, but the sub menu does not does not accept input correctly or exit when enter is pressed like on the main menu
here is the script:

Code:

#!/bin/sh
show_menu(){
    NORMAL=`echo "\033[m"`
    MENU=`echo "\033[36m"` #Blue
    NUMBER=`echo "\033[33m"` #yellow
    FGRED=`echo "\033[41m"`
    RED_TEXT=`echo "\033[31m"`
    ENTER_LINE=`echo "\033[33m"`
    echo -e "${MENU}*********************************************${NORMAL}"
    echo -e "${MENU}**${NUMBER} 1)${MENU} Option1 ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 2)${MENU} Option2 ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 3)${MENU} Option3 ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 4)${MENU} Option4 ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 5)${MENU} Sub Menu 1 ${NORMAL}"
    echo -e "${MENU}*********************************************${NORMAL}"
    echo -e "${ENTER_LINE}Please enter a menu option and enter or ${RED_TEXT}enter to exit. ${NORMAL}"
    read opt
}
function option_picked() {
    COLOR='\033[01;31m' # bold red
    RESET='\033[00;00m' # normal white
    MESSAGE=${@:-"${RESET}Error: No message passed"}
    echo -e "${COLOR}${MESSAGE}${RESET}"
}

sub_menu1(){
    NORMAL=`echo "\033[m"`
    MENU=`echo "\033[36m"` #Blue
    NUMBER=`echo "\033[33m"` #yellow
    FGRED=`echo "\033[41m"`
    RED_TEXT=`echo "\033[31m"`
    ENTER_LINE=`echo "\033[33m"`
    echo -e "${MENU}*********************************************${NORMAL}"
    echo -e "${MENU}**${NUMBER} 1)${MENU} Sub Option1 ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 2)${MENU} Sub Option2 ${NORMAL}"
    echo -e "${MENU}*********************************************${NORMAL}"
    echo -e "${ENTER_LINE}Please enter a menu option and enter or ${RED_TEXT}enter to exit. ${NORMAL}"
    read sub1
}

clear
show_menu
while [ opt != '' ]
    do
    if [[ $opt = "" ]]; then
            exit;
    else
        case $opt in
        1) clear;
        option_picked "Option 1 Picked";
        show_menu;
        ;;

        2) clear;
            option_picked "Option 2 Picked";
                show_menu;
            ;;

        3) clear;
            option_picked "Option 3 Picked";
                echo "test"
            show_menu;
            ;;

        4) clear;
            option_picked "Option 4 Picked";
            show_menu;
            ;;
        5) clear;
            option_picked "Sub Menu 1";
            sub_menu1;
            ;;

        x)exit;
        ;;

        \n)exit;
        ;;

        *)clear;
        option_picked "Pick an option from the menu";
        show_menu;
        ;;
            esac 
            fi 
            done

sub_menu1
while [ sub1 != '' ]
    do
    if [[ $sub1 = "" ]]; then
            exit;
    else
        case $sub1 in
        1) clear;
        option_picked "Sub menu option 1";
            echo "test"
        sub_menu_admin;
        ;;

        2) clear;
        option_picked "Sub menu option 2";
            echo "test"
        sub_menu_admin;
        ;;

                3) clear;
        option_picked "Sub menu option 2";
                "quit") break ;;
        ;;
       
        x)exit;
        ;;

        \n)exit;
        ;;

        *)clear;
        option_picked "Pick an option from the menu";
        sub_menu1;
        ;;
    esac     
fi
done


thanks

druuna 01-19-2014 03:40 AM

The input checks should be part of the function and not part of the main body.

The first thing called in your script is the show_menu function. If you select 5 (Sub Menu 1) you exit the show_menu function and the first while loop is entered (which checks opt). From there you enter the sub_menu1 function. Once you leave the sub_menu1 you are back inside the first while loop and opt is checked (and not sub1). And now you are stuck in the first loop.

Have a look at this:
Code:

#!/bin/sh
show_menu(){
    NORMAL=`echo "\033[m"`
    MENU=`echo "\033[36m"` #Blue
    NUMBER=`echo "\033[33m"` #yellow
    FGRED=`echo "\033[41m"`
    RED_TEXT=`echo "\033[31m"`
    ENTER_LINE=`echo "\033[33m"`
    echo -e "${MENU}*********************************************${NORMAL}"
    echo -e "${MENU}**${NUMBER} 1)${MENU} Option1 ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 2)${MENU} Option2 ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 3)${MENU} Option3 ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 4)${MENU} Option4 ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 5)${MENU} Sub Menu 1 ${NORMAL}"
    echo -e "${MENU}*********************************************${NORMAL}"
    echo -e "${ENTER_LINE}Please enter a menu option and enter or ${RED_TEXT}enter to exit. ${NORMAL}"
    read opt

  while [ opt != '' ]
  do
    if [[ $opt = "" ]]; then
      exit;
    else
      case $opt in
      1) clear;
      option_picked "Option 1 Picked";
      show_menu;
      ;;

      2) clear;
      option_picked "Option 2 Picked";
      show_menu;
      ;;

      3) clear;
      option_picked "Option 3 Picked";
      echo "test"
      show_menu;
      ;;

      4) clear;
      option_picked "Option 4 Picked";
      show_menu;
      ;;
      5) clear;
      option_picked "Sub Menu 1";
      sub_menu1;
      ;;

      x)exit;
      ;;

      \n)exit;
      ;;

      *)clear;
      option_picked "Pick an option from the menu";
      show_menu;
      ;;
      esac
    fi
  done
}

function option_picked() {
    COLOR='\033[01;31m' # bold red
    RESET='\033[00;00m' # normal white
    MESSAGE=${@:-"${RESET}Error: No message passed"}
    echo -e "${COLOR}${MESSAGE}${RESET}"
}

sub_menu1(){
    NORMAL=`echo "\033[m"`
    MENU=`echo "\033[36m"` #Blue
    NUMBER=`echo "\033[33m"` #yellow
    FGRED=`echo "\033[41m"`
    RED_TEXT=`echo "\033[31m"`
    ENTER_LINE=`echo "\033[33m"`
    echo -e "${MENU}*********************************************${NORMAL}"
    echo -e "${MENU}**${NUMBER} 1)${MENU} Sub Option1 ${NORMAL}"
    echo -e "${MENU}**${NUMBER} 2)${MENU} Sub Option2 ${NORMAL}"
    echo -e "${MENU}*********************************************${NORMAL}"
    echo -e "${ENTER_LINE}Please enter a menu option and enter or ${RED_TEXT}enter to exit. ${NORMAL}"
    read sub1
  while [ sub1 != '' ]
  do
    if [[ $sub1 = "" ]]; then
      exit;
    else
      case $sub1 in
      1) clear;
      option_picked "Sub menu option 1";
      echo "test"
      sub_menu_admin;
      ;;

      2) clear;
      option_picked "Sub menu option 2";
      echo "test"
      sub_menu_admin;
      ;;

      x)exit;
      ;;

      \n)exit;
      ;;

      *)clear;
      option_picked "Pick an option from the menu";
      sub_menu1;
      ;;
      esac
    fi
  done
}

clear
show_menu

BTW: There's a typo in the sub1 while loop:
Code:

                3) clear;
        option_picked "Sub menu option 2";
                "quit") break ;;
        ;;

The red part should be one ;

sbrock 01-19-2014 04:00 AM

Hi Druuna,

thankyou for your explanation and solution it is greatly appreciated


thanks

druuna 01-19-2014 04:01 AM

You're welcome :)

BTW: Can you put up the [SOLVED] tag if you think this is solved.
- above the first post -> Please Mark this thread as solved if you feel a solution has been provided.
- -or- -
- first post -> Thread Tools -> Mark this thread as solved

kremenko 03-08-2024 02:12 PM

Follow up
 
hi, thanks for posting a nice menu/submenu example script. I know I am asking a simple question, but how would one pass first menu input value to second menu. I d like to see a trace of all menu options used before performing an action at the last level nested menu? opt is just a menu option not the value.

MadeInGermany 03-10-2024 02:53 PM

Remember previously picked menu options?
Store them in variables!
If you have one variable per option then you can see all combinations.

michaelk 03-10-2024 03:59 PM

kremenko, If you have a specific question it would be better if you started a new thread instead of asking in an old solved thread.


All times are GMT -5. The time now is 03:23 PM.