Bash SubMenu不会退回到Parent

时间:2017-09-18 21:16:23

标签: bash sh ubuntu-16.04

我正在尝试为本地计算机上的一些bash脚本构建一个“分类的”dialog

我想做的是有类似下面的“菜单”结构

Parent Menu1
    Child 1
    Child 2
    Child 3
Parent Menu2
    Child 1
    Child 2
    Child 3

现在发生的一切,如果我选择Parent Menu1,然后从中选择退出创建的子菜单,它只刷新子菜单。

我怎样才能让它“回到”Parent Menu1

CODE

#!/bin/bash

if [ "$(whoami)" != "root" ]; then
    echo "Only root can do this.";
    exit 1;
else

    DWIDTH=40
    DHEIGHT=13
    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; 

    # Silently install dialog
    apt -y install dialog >/dev/null 2>&1

    cmd=(dialog --clear --backtitle "Get You Online Commander" --title "Get You Online Commander" --menu "What would you like to do?" $DHEIGHT $DWIDTH 6)

    options=(0 "Install/Update the Commander"
             1 "Manage Sites"
             99 "EXIT")

    while true; do

        choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

        # If cancelled, drop the dialog
        if [ $? -ne 0 ]; then
            dialog --clear --title "Exit Commander" --infobox "We'll see you next time." 7 $DWIDTH
            sleep 2;
            clear;
            exit;
        fi;

        for choice in $choices; do
            case $choice in
                0)
                    #install/update GYO Commander
                    echo "Installer"
                    ;; 
                1) 
                    # manage sites
                    _c=(dialog --clear --backtitle "GYOC Site Manager" --title "GYOC Site Manager" --menu "What would you like to do?" $DHEIGHT $DWIDTH 5)
                    _opts=(1 "Create a New Site"
                             99 "BACK")
                    while true; do
                        _choices=$("${_c[@]}" "${_opts[@]}" 2>&1 >/dev/tty)
                        for _choice in $_choices; do
                            case $_choice in
                                1)
                                    # new site
                                    echo "new site"
                                    ;;
                                99|*)
                                    break;
                                    ;;
                            esac;
                        done;
                    done;
                    sleep 3;
                    ;;
                99|*)
                    dialog --title "Exit Commander" --infobox "We'll see you next time." 7 $DWIDTH
                    sleep 2;
                    clear;
                    exit;
                    ;;
            esac        
        done
    done
fi;

2 个答案:

答案 0 :(得分:1)

你被困在无限循环中(无意中):

  _choices=$("${_c[@]}" "${_opts[@]}" 2>&1 >/dev/tty)
  for _choice in $_choices; do
     case $_choice in
        1)
        # new site
        echo "new site"
        ;;
        99|*)
        break;
        ;;
      esac;
  done;
  ############## FIX ###############
  if [[ "$_choice" == 99 ]]; then
     break
  fi
  ##################################
  done;
  sleep 3
  ;;

答案 1 :(得分:0)

知道了。 它是二级菜单中的while true; do

最终代码

#!/bin/bash

if [ "$(whoami)" != "root" ]; then
    echo "Only root can do this.";
    exit 1;
else

    DWIDTH=40
    DHEIGHT=13
    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; 

    # Silently install dialog
    apt -y install dialog >/dev/null 2>&1

    cmd=(dialog --clear --backtitle "Get You Online Commander" --title "Get You Online Commander" --menu "What would you like to do?" $DHEIGHT $DWIDTH 6)

    options=(0 "Install/Update the Commander"
             1 "Manage Sites"
             99 "EXIT")

    while true; do

        choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

        # If cancelled, drop the dialog
        if [ $? -ne 0 ]; then
            dialog --clear --title "Exit Commander" --infobox "We'll see you next time." 7 $DWIDTH
            sleep 2;
            clear;
            exit;
        fi;

        for choice in $choices; do
            case $choice in
                0)
                    #install/update GYO Commander
                    echo "Installer"
                    ;; 
                1) 
                    # manage sites
                    _c=(dialog --clear --backtitle "GYOC Site Manager" --title "GYOC Site Manager" --menu "What would you like to do?" $DHEIGHT $DWIDTH 5)
                    _opts=(1 "Create a New Site"
                             99 "BACK")
                    #while true; do
                    _choices=$("${_c[@]}" "${_opts[@]}" 2>&1 >/dev/tty)
                    for _choice in $_choices; do
                        case $_choice in
                            1)
                                # new site
                                echo "new site"
                                ;;
                            99|*)
                                break;
                                ;;
                        esac;
                    done;
                    #done;
                    sleep 3;
                    ;;
                99|*)
                    dialog --title "Exit Commander" --infobox "We'll see you next time." 7 $DWIDTH
                    sleep 2;
                    clear;
                    exit;
                    ;;
            esac        
        done
    done
fi;