1

Running into an issue with exiting a submenu. Check it out:

function submenu
{
select submenu1 in "Submenu 1" "Submenu 2" "Submenu 3" "Exit to main menu"; do
    case $submenu1 in
        "Submenu 1" ) echo "SubMenu1"; ;;
        "Submenu 2" ) echo "SubMenu1"; ;;
        "Submenu 3" ) echo "SubMenu1"; ;;
        "Exit to main menu" ) break;;
        * ) echo "Please select an option."; ;;
    esac
done
}

select mainMenu in "Main Menu 1" "Main Menu 2" "Main Menu 3" "Quit"; do
    case $mainMenu in
        "Main Menu 1" ) submenu; ;;
        "Main Menu 2" ) echo "MainMenu2"; ;;
        "Main Menu 3" ) echo "MainMenu3"; ;;
        Quit ) exit;;
        * ) echo "Please select an option.";
    esac
done

If a user selects 1 from the main menu, the submenu function kicks off. If the user then types "4" and hits enter, it just sits there. If they hit enter again, THEN it pops back to the main menu.

Not sure what I'm missing, I feel it's something simple.

3
  • You actually return to the main menu. It is just not printed (that is the behavior of select). Try to use 2) after using 4) in the submenu :) – PesaThe Dec 19 '17 at 23:23
  • Gotcha - so what I need to do is figure out how to reprint the menu after the option is selected. Thanks! – guestAccountThanks Dec 20 '17 at 0:00
  • You can create your own easy menu. Take a look at read to get user input and that's all there is to it. – PesaThe Dec 20 '17 at 0:01
1

The only thing that you're missing is that this is a fully intended behavior.

When you are in the main menu, and you press 2, what happens?

1) Main Menu 1
2) Main Menu 2
3) Main Menu 3
4) Quit
#? 2
MainMenu2
#?

The echo "MainMenu2" command is executed, and then the #? prompt is redisplayed. Nothing else. The selected command was executed, and the #? prompt redisplayed. If you press enter again (empty input), the menu options are redisplayed.

If you go into the sub-menu, and then come out of it, you get a consistent behavior: the #? prompt is redisplayed. If you press enter again (empty input), the menu options are redisplayed.

1

To force the redisplay of the menu, add an outer loop around the select and always break out of the select loop when an option has been selected. Also, don't use the actual menu items in the case ... esac statement, but the numbers, which make the code more readable and also more easily maintainable.

submenu() {
    while true; do
        select submenu1 in 'Submenu 1' 'Submenu 2' 'Submenu 3' 'Exit to main menu'
        do
            case $REPLY in
                1) echo 'SubMenu1' ;;
                2) echo 'SubMenu2' ;;
                3) echo 'SubMenu3' ;;
                4) break 2 ;;
                *) echo 'Please select an option.' >&2
            esac
            break
        done
    done
}

while true; do
    select mainMenu in 'Main Menu 1' 'Main Menu 2' 'Main Menu 3' 'Quit'
    do
        case $REPLY in
            1) submenu ;;
            2) echo 'MainMenu2' ;;
            3) echo 'MainMenu3' ;;
            4) break 2 ;;
            *) echo 'Please select an option.' >&2
        esac
        break
    done
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.