Score:0

Shell script not exiting sub-menu, help!

ca flag

I've got a problem with a shell script I'm writing in that it does everything its supposed to do except it won't exit from sub menus nor will it quit from the script completely when in a sub menu.

Here is what I've done so far. I've redacted parts of it because of work confidentiality:

#!/bin/bash



#Now for the main menu...

while :
do

    clear
    cat<<EOF
    ======================================
    DCO Toolbox 0.5a
    ======================================
    If it aint broke, fix it till it is
    --------------------------------------
    Welcome!

    Please choose which site you're in:

    A new or existing site A   (1) 
    A new or existing site B      (2)
    --------------------------------------

EOF
(   read -n1 -s
     case "$REPLY" in
    "1") echo "Going to that menu now"
    sleep 1
    while :
do
    clear
    cat<<EOF
    ======================================
    DCO Toolbox 0.5a
    =======================================
    SITE A MENU
    ---------------------------------------
    Please make a choice:

    Change DA                   (1)
    Change Site                 (2)
    Change Rack number          (3)
    Test DA port connectivity   (4)
    Test DA port light levels   (5)
    Check DS port order         (6)
    Check DS LLDP neighbors     (7)
                                (Q)uit
    ---------------------------------------

EOF
(    read -n1 -s
case "$REPLY" in
    "1") echo "Lets change that DA/DS, enter a new one: "
    read da 
    echo "Setting that now.."
    ;;
    "2") echo "Lets change the site name, enter a new one: "
    read site 
    echo "Setting that now.."
    ;;
    "3") echo "Lets change the target rack, enter a new one: "
    read rack 
    echo "Setting that now.."
    ;;
    "4") echo "Testing DA port connectivity..."
    sshpass -p $password ssh $username@$da.$site "sh int et21/1-24/4 status"
    read -rsp $'Press any key to continue...\n' -n1 key
    ;;
    "5") echo "Testing DA port light levels..."
    sshpass -p $password ssh $username@$da.$site "sh int et21/1-24/4 transceiver"
    read -rsp $'Press any key to continue...\n' -n1 key
    ;;
    "6") 
    sshpass -p $password ssh $username@ds01.$site "sh int desc | inc $rack"
    sshpass -p $password ssh $username@ds02.$site "sh int desc | inc $rack" 
    sshpass -p $password ssh $username@ds03.$site "sh int desc | inc $rack" 
    sshpass -p $password ssh $username@ds04.$site "sh int desc | inc $rack" 
    read -rsp $'Press any key to continue...\n' -n1 key
    ;;
    "7") echo "Checking DS LLDP neighbors..."
    sshpass -p $password ssh $username@ds01.$site "sh lldp neigh | inc $rack" 
    sshpass -p $password ssh $username@ds02.$site "sh lldp neigh | inc $rack" 
    sshpass -p $password ssh $username@ds03.$site "sh lldp neigh | inc $rack" 
    sshpass -p $password ssh $username@ds04.$site "sh lldp neigh | inc $rack" 
    read -rsp $'Press any key to continue...\n' -n1 key

    ;;
    "Q") break 2                                ;;
    "q") echo -e $blinkred"ERROR: "$red"Whoa! Case sensitive, mate!" $none  ;;
    * ) echo "Sorry, invalid option!"        ;;
    esac

    sleep 1 ) | tee $today.log
done
break
    ;;
    "2") echo "Going to that menu now"
    sleep 1
    while :
do
    clear
    cat<<EOF
    ======================================
    DCO Toolbox 0.5a
    =======================================
    SITE B MENU
    ---------------------------------------
    Please make a choice:

    Change Device                   (1)
    Change Site                     (2)
    Change Rack number              (3)
    Test port connectivity          (4)
    Test port light levels          (5)
    Check port order                (6)
    Check LLDP neighbors            (7)
                                    (Q)uit
    ---------------------------------------

EOF
(    read -n1 -s
    case "$REPLY" in
    "1") echo "Lets change that DA/DS, enter a new one: "
    read da 
    echo "Setting that now.."
    ;;
    "2") echo "Lets change the site name, enter a new one: "
    read site 
    echo "Setting that now.."
    ;;
    "3") echo "Lets change the target rack, enter a new one: "
    read rack 
    echo "Setting that now.."
    ;;
    "4") echo "Testing DA port connectivity..."
    sshpass -p $password ssh $username@$da.$site "sh int et21/1-24/4 status"
    read -rsp $'Press any key to continue...\n' -n1 key
    ;;
    "5") echo "Testing DA port light levels..."
    sshpass -p $password ssh $username@$da.$site "sh int et21/1-24/4 transceiver"
    read -rsp $'Press any key to continue...\n' -n1 key
    ;;
    "6") 
    sshpass -p $password ssh $username@ds01.$site "sh int desc | inc $rack"
    sshpass -p $password ssh $username@ds02.$site "sh int desc | inc $rack" 
    sshpass -p $password ssh $username@ds03.$site "sh int desc | inc $rack" 
    sshpass -p $password ssh $username@ds04.$site "sh int desc | inc $rack" 
    read -rsp $'Press any key to continue...\n' -n1 key
    ;;
    "7") echo "Checking DS LLDP neighbors..."
    sshpass -p $password ssh $username@ds01.$site "sh lldp neigh | inc $rack" 
    sshpass -p $password ssh $username@ds02.$site "sh lldp neigh | inc $rack" 
    sshpass -p $password ssh $username@ds03.$site "sh lldp neigh | inc $rack" 
    sshpass -p $password ssh $username@ds04.$site "sh lldp neigh | inc $rack" 
    read -rsp $'Press any key to continue...\n' -n1 key

    ;;
    "Q") break 2                                ;;
    "q") echo -e $blinkred"ERROR: "$red"Whoa! Case sensitive, mate!" $none  ;;
    * ) echo "Sorry, invalid option!"        ;;
    esac

    sleep 1 ) | tee $today.log
done
break
    ;;
esac
)
done

I've got one main menu and two sub-menus:

Main menu
 |
 |_____ Site A Sub Menu
 |
 |_____ Site B Sub Menu

With an option (Q) to quit back to the main menu but I can't seem to be able to quit back to the Main menu. I've tried:

break 2 ;;

And:

return ;;

But both don't seem to work at all. The script just loops back around within the same sub-shell.

Any help would be very gratefully received. Thanks!

FedKad avatar
cn flag
Why are you using sub-shells?
ca flag
Because I felt that was the way to go. What would you do instead? @FedKad
bac0n avatar
cn flag
remove them....
FedKad avatar
cn flag
You can start by removing the sub-shells. However, your script has many syntax and logic errors. It will be a good start to check it at https://www.shellcheck.net/ . And please note that this is a Q & A site and you may not get help in script debugging.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.