2016-08-10 33 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 
     ;; 
...