0

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]\n\n1. cPanel\n2. Plesk\n3. Ubuntu\n4. No Panel / Simple Control Panel\n\nEnter Selection: "
read type
while true; do
case $type in
    "1")
        echo -en "Here is the stuff we can do for a cPanel server\n\n1. do this\n2. do that\n\nEnter 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!

3
  • You should take a look at linux.die.net/man/1/dialog This is not directly related to your question but I guess it is helpfull anyway because it offers nice menus and depending on the terminal dialog has even mouse support. Commented Apr 10, 2015 at 13:28
  • Break will only break the current loop. Commented Apr 10, 2015 at 13:36
  • Have you seen the select loop? Commented Apr 10, 2015 at 20:55

2 Answers 2

1

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 server\n\n1. do this\n2. do that\n\nEnter 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

Sign up to request clarification or add additional context in comments.

1 Comment

Hey thanks man! This was a great learning experience. Thanks!
1

Here is the new, perfectly navigated code:

#!/bin/bash
while true; do
    echo -en "\nWhat type of server is this? [DIGIT ONLY]\n\n1. cPanel\n2. Plesk\n3. Ubuntu\n4. No Panel / Simple Control Panel\n5.) Exit\n\nEnter Selection: "
    read type
    case $type in
            "1")
                    while true; do
                            echo -en "Here is the stuff we can do for a cPanel server\n\n1. do this\n2. do that\n3. Go Back\n4. Exit\n\nEnter 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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.