skip to Main Content

Okay, so I have this simple menu-driven BASH script. I’m still learning BASH, so “KISS” is my method, for the moment. The code below works, so that’s cool … take a look, i’ll explain my issue on the other side …

echo -en "nWhat type of server is this? [DIGIT ONLY]nn1. cPaneln2. Pleskn3. Ubuntun4. No Panel / Simple Control PanelnnEnter Selection: "
read type
while true; do
case $type in
    "1")
        echo -en "Here is the stuff we can do for a cPanel servernn1. do thisn2. do thatnnEnter Selection: ";
        read cpopt
            case $cpopt in
                "1")
                    echo "sub1"
                    break
                ;;
                "2")
                    echo "sub2"
                    break
                ;;
                *)
                    echo "You did not choose an option, lets go back!"
                    break
                ;;
            esac;;
    "2")
        echo "Plesk Stuff";
        break
    ;;
    "3")
        echo "Ubuntu Stuff";
        break
    ;;
    "4")
        echo "No Panel / Simple Control Panel";
        break
    ;;
    *)
        echo "Invalid Input";
        break
    ;;
esac
done

As you can see, each CASE statement works (so that’s good), but here is what I’m trying to do … I want to create a simple navigation system … something that allows users 5-Levels deep to go back one level (or quit altogether). As you can see, I’m using BREAK, but that quits the whole script, so then I thought I would use a nested WHILE, thinking the BREAK would only break out of the loop it was in (the nested one) … no joy, that also killed the whole script.

Since I’m learning, I’m not here looking for a definitive answer (although those are nice) … I’m really looking for “You should read about XYZ, that will do that) … and then I’ll dive into that.

I very much appreciate any help that is offered, thank you!

2

Answers


  1. Chosen as BEST ANSWER

    Here is the new, perfectly navigated code:

    #!/bin/bash
    while true; do
        echo -en "nWhat type of server is this? [DIGIT ONLY]nn1. cPaneln2. Pleskn3. Ubuntun4. No Panel / Simple Control Paneln5.) ExitnnEnter Selection: "
        read type
        case $type in
                "1")
                        while true; do
                                echo -en "Here is the stuff we can do for a cPanel servernn1. do thisn2. do thatn3. Go Backn4. ExitnnEnter Selection: ";
                                read cpopt
                                        case $cpopt in
                                                "1")
                                                        echo "Did This!"
                                                        sleep 1
                                                        clear
                                                        continue
                                                        ;;
                                                "2")
                                                        echo "Did That!"
                                                        sleep 1
                                                        clear
                                                        continue
                                                ;;
                                                "3" )
                                                        echo "Going Back!"
                                                        sleep 1
                                                        clear
                                                        break
                                                ;;
                                                "4" )
                                                        echo "Exiting..."
                                                        sleep 1
                                                        break 2
                                                ;;
                                                *)
                                                        echo "Invalid Input";
                                                        sleep 1
                                                        clear
                                                        continue
                                                ;;
                                        esac
                        done
                        ;;
                "2")
                        echo "Plesk Stuff";
                        sleep 1
                        clear
                        continue
                ;;
                "3")
                        echo "Ubuntu Stuff";
                        sleep 1
                        clear
                        continue
                ;;
                "4")
                        echo "No Panel / Simple Control Panel";
                        sleep 1
                        clear
                        continue
                ;;
                "5")
                        echo "Exiting..."
                        sleep 1
                        break
                ;;
                *)
                        echo "Invalid Input";
                        sleep 1
                        clear
                        continue
                ;;
        esac
    done
    

    Thanks, I really appreciate the help!


  2. You have nested cases but not looped them so it will just carry on the first case statement after the second

    Something like this should work

    #/bin/bash
    
    while true;do
            echo "Input type"
            read type
    
            case $type in
    
            "1")
                    while true;do
                            echo -en "Here is the stuff we can do for a cPanel servernn1. do thisn2. do thatnnEnter Selection: ";
                            read cpopt
                            case $cpopt in
                            "1")
                                    echo "sub1"
                                    break
                                    ;;
                            "2")
                                    echo "sub2"
                                    break
                                    ;;
                            *)
                                    echo "You did not choose an option, lets go back!"
                                    ;;
                            esac
                    done
                    ;;
    
            "2")
                    echo "Plesk Stuff";
                    break
                    ;;
            "3")
                    echo "Ubuntu Stuff";
                    break
                    ;;
            "4")
                    echo "No Panel / Simple Control Panel";
                    break
                    ;;
            *)
                    echo "Invalid Input";
                    ;;
            esac
    done
    

    You will need to create a new loop for each sub menu and also omit the breaks from options you want to loop like and incorrectly selected option.

    Furthermore if you want to move up multiple level of loops(or menus) then you can put a number after the break

    i.e

    break 2
    

    This will move back through two levels of loops


    For further reading on loop control you can look here
    http://tldp.org/LDP/abs/html/loopcontrol.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search