LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2014, 02:47 AM   #1
sbrock
LQ Newbie
 
Registered: Jan 2014
Posts: 2

Rep: Reputation: Disabled
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
 
Old 01-19-2014, 03:40 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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 ;
 
1 members found this post helpful.
Old 01-19-2014, 04:00 AM   #3
sbrock
LQ Newbie
 
Registered: Jan 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Hi Druuna,

thankyou for your explanation and solution it is greatly appreciated


thanks
 
Old 01-19-2014, 04:01 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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
 
Old 03-08-2024, 02:12 PM   #5
kremenko
LQ Newbie
 
Registered: Mar 2024
Posts: 3

Rep: Reputation: 0
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.
 
Old 03-10-2024, 02:53 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
Remember previously picked menu options?
Store them in variables!
If you have one variable per option then you can see all combinations.
 
Old 03-10-2024, 03:59 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,736

Rep: Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script choice menu from csv file deefke Linux - Newbie 5 01-25-2011 01:31 PM
Bash script help... menu to pick file from folder bfellenr Linux - Newbie 13 01-15-2011 03:30 PM
Making menu's within menu's (function) in bash genderbender Programming 1 03-19-2008 10:12 AM
making select show its menu in a bash script? zidane_tribal Programming 6 05-02-2005 05:52 AM
How logout from login-menu bash script? MikHud Linux - General 0 04-12-2002 12:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration