Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to create more menus under option "a" when selected. If the user entered option "a" I want it to have a couple menus for you to select and for one of the options is to return to the "Main Menu". This is the 2 problems I'm having trouble with. I tried doing this same format I have below for the menu here and put it under "A" block of code but that didn't work.

#!/bin/bash

ok=0;

while ((ok==0))
do
echo "Main Menu:"
echo -e "\t(a) More Menu Options "
echo -e "\t(b) Exit"
echo -n "Please enter your choice:"
read choice
case $choice in
    "a"|"A")
    ok=1

    ;;
    "b"|"B")
    exit
    ;;
        *)
        echo "invalid answer, please try again"
        ;;

esac
done
share|improve this question

2 Answers 2

up vote 3 down vote accepted

Why not put the same cycle inside?

#!/bin/bash

while :
do
echo "Main Menu:"
echo -e "\t(a) More Menu Options "
echo -e "\t(b) Exit"
echo -n "Please enter your choice:"
read choice
case $choice in
    "a"|"A")
    while :
    do
    echo "Secondary menu"
    echo -e "\t(c) Print this menu again"
    echo -e "\t(d) Return to main menu"
    echo -n "Please enter your choice:"
    read choice1
    case $choice1 in
        "c"|"C")
        ;;
        "d"|"D")
        break
        ;;
            *)
            echo "invalid answer, please try again"
            ;;
    esac
    done
    ;;
    "b"|"B")
    exit
    ;;
        *)
        echo "invalid answer, please try again"
        ;;

esac
done

There is also select command which creates menus:

select i in ant bee cat
do 
    echo $i
    break
done

Let's run it:

$ select i in ant bee cat; do echo $i; break; done
1) ant
2) bee
3) cat
#? 2
bee
share|improve this answer

Try this:

#!/bin/bash

submanual(){
while :
do
echo "sub Menu:"
echo -e "\t(x) Options 1"
echo -e "\t(y) Options 1"
echo -e "\t(e) Back"
echo -n "Please enter your choice:"
read c
case $c in
    "x"|"X")
    # Options 1 and its commands
    ;;
    "y"|"Y")
    # Options 2 and its commands
    ;;
    "e"|"E")
    break
    ;;
        *)
        echo "invalid answer, please try again"
        ;;
esac
done
}

while :
do
echo "Main Menu:"
echo -e "\t(a) More Menu Options "
echo -e "\t(b) Exit"
echo -n "Please enter your choice:"
read choice
case $choice in
    "a"|"A")
    submanual
    ;;
    "b"|"B")
    exit
    ;;
        *)
        echo "invalid answer, please try again"
        ;;

esac
done
share|improve this answer
    
Did you read my code carefully before you vote it down? Seems you are not shell guy at all. –  BMW Mar 27 at 4:30
    
really, not show the manual again? did you really test it? If not, do that, then answer me. –  BMW Mar 27 at 4:33
    
Man, How can I know what OP need put there to show more options. I have to leave the space for OP –  BMW Mar 27 at 4:38
    
fix the infinite loop issue, then OP should know how to handle the submanual. –  BMW Mar 27 at 5:09
    
Thank you for the edit. Now you are actually answering the question and your code works as expected. I will delete my comments (I suggest you do the same since they are no longer relevant) and have changed my downvote to an upvote. –  terdon Mar 27 at 5:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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