我已经为一个新用户创建了一个循环来执行一些虚拟机设置功能,例如添加主机名、ip地址等。但是,如果我在任何whiptail窗口中单击“Cancel”按钮,它就会移动到循环中的下一个whiptail元素。如果选择了Cancel,我如何设置它来取消循环并返回主菜单窗口?
while true
do
OPTION=$(whiptail --title "Configuration Menu" --menu "Choose an option" 20 78 10 \
"1" "Show current configuration." \
"2" "Setup Wizard." \
...
"0" "EXIT" 3>&1 1>&2 2>&3)
exitstatus=$?
case "$OPTION" in
...
2)
# Setup hostname
HOSTNAME=$(whiptail --inputbox "Hostname" 8 78 `hostname` --title "Serial Number" 3>&1 1>&2 2>&3)
...
# IP address configuration
IP_CONFIG=$(whiptail --title "Network Configuration" --radiolist "Choose a configuration option" 20 78 10 \
"DHCP" "Use Dynamic Host Protocol" ON \
"STATIC" "Configure Static IP" OFF 3>&1 1>&2 2>&3)
...
;;
esac下面是主菜单的外观:

如果单击第一个输入框中的“取消”...

我被发送到下一个whiptail元素,而不是取消到主菜单:

发布于 2016-08-11 01:12:51
好了,我想通了。我必须将每个案例选项包装在它自己的exitstatus = 0测试中:
2)
# Setup serial number
SERIAL=$(whiptail --inputbox "Serial Number" 8 78 `hostname` --title "Serial Number" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then
...
else
break
fi
;;
...发布于 2016-08-10 23:22:40
使用退出循环的break。选择<Cancel>时,whiptail返回1,因此进行测试,如果是,则返回break:
OPTION=$(whiptail --title "Configuration Menu" --menu "Choose an option" 20 78 10 \
"1" "Show current configuration." \
"2" "Setup Wizard." \
...
"0" "EXIT" 3>&1 1>&2 2>&3)
exitstatus=$?
[[ "$exitstatus" = 1 ]] && break;https://stackoverflow.com/questions/38877447
复制相似问题