tags:

views:

236

answers:

3

I have A Script that has a Select statement to go to multiple sub select statements however once there I can not seem to figure out how to get it to go back to the main script. also if possible i would like it to re-list the options

  #!/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
+1  A: 

you use a loop to do that..

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
ghostdog74
A: 

The bourne shell has a useful construct that I sometimes wish C had.

You can break out of nested control structures with a "break n", where n can be 2, 3, etc.

So, from your nested sub-select, you can issue a break 2; to get back to the top level. I'm not entirely positive what you're trying to achieve though.

janks
+2  A: 

You would generally nest case statements within select statements and put the whole thing in a loop:

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
Dennis Williamson