我有一个脚本,有一个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
Run Code Online (Sandbox Code Playgroud)
您通常会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
Run Code Online (Sandbox Code Playgroud)