2016-08-10 45 views
1

我已经创建了一个循环来通过一些新用户的虚拟机设置功能,它经历了像添加主机名,IP地址等东西。但是,如果我点击'取消'按钮在任何whiptail窗口中,它只是移动到循环中的下一个whiptail元素。如果选择Cancel,我该如何设置它来取消循环并返回到主菜单窗口?Whiptail输入框取消不起作用

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 

这里是主菜单的样子: enter image description here

如果在第一输入框中输入“取消”,当点击... enter image description here

我发送到下一个whiptail元素而不是取消到主菜单: enter image description here

回答

0

使用break退出循环。 whiptail返回1上<Cancel>选择,因此测试是什么,以及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; 
+0

我应该放置'$退出状态='试验在每种情况下选项或正下方初步'$ exitstatus'变量? – Godzilla74

+0

'$ exitstatus'应该在下面。 –

+0

不,它仍会前进到下一个窗口,而不是返回主菜单 – Godzilla74

1

好了,想通了。我不得不换每种情况下选择在自己的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 
     ;; 
...