【问题标题】:Bash Shell Script: Nested Select StatementsBash Shell 脚本:嵌套选择语句
【发布时间】:2010-04-08 00:29:27
【问题描述】:

我有一个脚本,它有一个 Select 语句可以转到多个子 select 语句,但是一旦出现,我似乎无法弄清楚如何让它返回到主脚本。如果可能的话,我希望它重新列出选项

  #!/bin/bash
           PS3='Option = '
           MAINOPTIONS="Apache Postfix Dovecot All Quit"
           APACHEOPTIONS="Restart Start Stop Status"
           POSTFIXOPTIONS="Restart Start Stop Status"
           DOVECOTOPTIONS="Restart Start Stop Status"
           select opt in $MAINOPTIONS; do
               if [ "$opt" = "Quit" ]; then
               echo Now Exiting
               exit
               elif [ "$opt" = "Apache" ]; then
                 select opt in $APACHEOPTIONS; do
                 if [ "$opt" = "Restart" ]; then
                 sudo /etc/init.d/apache2 restart
                 elif [ "$opt" = "Start" ]; then
                 sudo /etc/init.d/apache2 start
                 elif [ "$opt" = "Stop" ]; then
                 sudo /etc/init.d/apache2 stop
                 elif [ "$opt" = "Status" ]; then
                 sudo /etc/init.d/apache2 status
                 fi
                 done
               elif [ "$opt" = "Postfix" ]; then
                 select opt in $POSTFIXOPTIONS; do
                 if [ "$opt" = "Restart" ]; then
                 sudo /etc/init.d/postfix restart
                 elif [ "$opt" = "Start" ]; then
                 sudo /etc/init.d/postfix start
                 elif [ "$opt" = "Stop" ]; then
                 sudo /etc/init.d/postfix stop
                 elif [ "$opt" = "Status" ]; then
                 sudo /etc/init.d/postfix status
                 fi
                 done
               elif [ "$opt" = "Dovecot" ]; then
                 select opt in $DOVECOTOPTIONS; do
                 if [ "$opt" = "Restart" ]; then
                 sudo /etc/init.d/dovecot restart
                 elif [ "$opt" = "Start" ]; then
                 sudo /etc/init.d/dovecot start
                 elif [ "$opt" = "Stop" ]; then
                 sudo /etc/init.d/dovecot stop
                 elif [ "$opt" = "Status" ]; then
                 sudo /etc/init.d/dovecot status
                 fi
                 done
                 elif [ "$opt" = "All" ]; then
                 sudo /etc/init.d/apache2 restart
                 sudo /etc/init.d/postfix restart
                 sudo /etc/init.d/dovecot restart
               fi
               done

【问题讨论】:

    标签: bash shell select


    【解决方案1】:

    您通常会将case 语句嵌套在select 语句中,并将整个内容置于一个循环中:

    while true
    do
        select option in $options
        do
            case $option in
                choice1)
                    do_something
                    ;;
                choice2)
                    select sub_option in $sub_options
                    do
                        case $sub_option in
                            sub_choice1)
                                another_thing
                                ;;
                            sub_choice2)
                                break    # return to current (sub) menu
                                ;;
                            sub_choice3)
                                break 2  # return to parent menu
                                ;;
                         esac
                choice3)
                     break    # return to current (main) menu
                     ;;
                choice4)
                     break 2  # exit the while loop so cleanup can be done at the end of the script
            esac
        done
    done
    do_cleanup
    

    【讨论】:

      【解决方案2】:

      您使用循环来执行此操作..

      while true
      do
      ...
      ...
        read -p "do you want to continue (Q)uit?" choice
        case "$choice" in
          Q|q) break;; #or exit your script
        esac
      ...
      done
      

      【讨论】:

        【解决方案3】:

        bourne shell 有一个有用的结构,我有时希望 C 有。

        您可以使用“break n”打破嵌套的控制结构,其中 n 可以是 2、3 等。

        因此,从您的嵌套子选择中,您可以发出 break 2; 以返回顶层。不过,我并不完全肯定您要实现的目标。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-06-10
          • 1970-01-01
          • 1970-01-01
          • 2017-09-15
          • 2021-04-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多