Question

I am trying to make a complete BASH menu with submenus via select opt in.

The problem : When I go to a submenu then come back to the initial menu, it do not show options.

----------------------------------------------
          Greenwatch's Kiosk debug menu
----------------------------------------------

1) Keyboard Layout,             5) Configure Kiosk's password,
2) Timezone configuration,      6) Set Proxy,
3) -,                           7) Remove Proxy
4) Launch Kiosk anyway,
Enter your choice (mainmenu), press 0 to reboot: 1


1) Azerty layout (BE)
2) Querty layout (US)
3) Cancel
Enter your choice (submenu): 1
AZERTY Keyboard configured

Enter your choice (mainmenu), press 0 to reboot: 

This is the code(simplified -with only one submenu- )

choose_keyboard() {
    show_title "Choose your keyboard layout"
    clear;
    select opt in "Azerty layout (BE)" "Querty layout (US)" "Cancel"; do
        case "$REPLY" in
            1 ) loadkeys be-latin1; echo "AZERTY Keyboard configured"; break;;
            2 ) loadkeys us; echo "QWERTY Keyboard configured"; break;;
            3 ) echo "Canceled"; break;;
            777 ) break;;
            *) echo "This is not a valid option, retry";;
        esac
    done
}
main_menu() {
    show_title "$title"
    select opt in "${options[@]}"; do 
        case "$REPLY" in
            0 ) show_title "See you as late as possible!"; sudo systemctl reboot;;
            1 ) choose_keyboard;;
            2 ) choose_timezone;;
            3 ) lauch_kiosk;;
            4 ) choose_password;;
            5 ) choose_ipconfig;;
            6 ) choose_proxy;;
            7 ) choose_testlab;;
            777 ) break;;
            *) echo "This is not a valid option, retry";;
        esac
    done
}
main_menu

How could I force select to display the menu? NOTE: If I call main_menu into the choose_keyboard function, I will certainly obtain a stackoverflow error!

Was it helpful?

Solution

When you break from the inner select, you re-enter the top (main menu) select - as you have discovered, the menu isn't displayed because you don't re-execute the commands at the beginning of the function. Instead, you can break out of the inner and outer selects at once, and have the main menu in a loop so that it gets called again, ie:

1 ) loadkeys be-latin1; echo "AZERTY Keyboard configured"; break 2;;

break 2 will break out of a select nested inside another, break 3 will break out of an additional level of nesting, etc. Then instead of just calling main_menu at the bottom, do something like:

while :; do main_menu; done

This is an infinite loop which will call main_menu whenever you break out of the main menu select command. You may not want it to be infinite, you can always test against a variable or something there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top