Bash Shell脚本:嵌套的选择语句

3

我有一个脚本,其中包含一个SELECT语句,用于进入多个子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
3个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
8

通常情况下,您会在select语句中嵌套case语句,并将整个内容放在循环中:

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

3
你可以使用循环来完成这个任务。
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

2

伯恩 shell 有一个很有用的结构,我有时希望 C 也有。

您可以使用 "break n" 来退出嵌套控制结构,其中 n 可以是 2、3 等。

因此,从嵌套子选择中,您可以发出 break 2; 来返回顶层。但是我不完全确定您想要实现什么。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,