1. 程式人生 > >使用whiptail在shell指令碼中建立互動式對話方塊?

使用whiptail在shell指令碼中建立互動式對話方塊?

當你在終端環境下安裝新的軟體時,你可以經常看到資訊對話方塊彈出,需要你的輸入。對話方塊的型別有密碼箱,檢查表,選單,等等。他們可以引導你以一種直觀的方式輸入必要的資訊,使用這樣的使用者友好的對話方塊的好處是顯而易見的。如下圖所示:

shell-1

當你寫一個互動式shell指令碼,你可以使用這樣的對話方塊來接受使用者的輸入。whiptail可以shell指令碼中建立基於終端的對話方塊,訊息框的過程,類似於Zenityxdialog GUI指令碼程式碼。預先安裝在所有的Linux釋出版本中。

下面來看看whiptail的用法:

建立一個訊息框

一個訊息框中顯示一個確認按鈕繼續任意的文字訊息

語法:

whiptail –title “<message box title>” –msgbox “<text to show>” <height> <width>

例項:

#!/bin/bash whiptail --title "Test Message Box" --msgbox "Create a message box with whiptail. Choose Ok to continue." 10 60

shell-2

建立一個yes/no對話方塊

使用者輸入yes或no的對話方塊。

語法:

whiptail –title “<dialog box title>” –yesno “<text to show>” <height> <width>

例項:

#!/bin/bash if
(whiptail --title "Test Yes/No Box" --yesno "Choose between Yes and No." 10 60) then echo "You chose Yes. Exit status was $?." else echo "You chose No. Exit status was $?." fi

shell-3

或者,你可以是“–yes-button” ,”–no-button”選項。

#!/bin/bash if (whiptail --title "Test Yes/No Box" --yes-button "Skittles" --no-button
"M&M's"  --yesno "Which do you like better?" 10 60) then echo "You chose Skittles Exit status was $?." else echo "You chose M&M's. Exit status was $?." fi

shell-4

建立一個表單輸入

如果你想使用者輸入任意的文字您可以使用一個輸入框

語法:

whiptail –title “<input box title>” –inputbox “<text to show>” <height> <width> <default-text>

例項:

#!/bin/bash PET=$(whiptail --title "Test Free-form Input Box" --inputbox "What is your pet's name?" 10 60 Wigglebutt 3>&1 1>&2 2>&3) exitstatus=$? if [ $exitstatus = 0 ]; then echo "Your pet name is:" $PET else echo "You chose Cancel." fi

shell-5

建立一個密碼框

使用者需要輸入敏感資訊時密碼框是有用的

語法:

whiptail –title “<password box title>” –passwordbox “<text to show>” <height> <width>

例項:

#!/bin/bash PASSWORD=$(whiptail --title "Test Password Box" --passwordbox "Enter your password and choose Ok to continue." 10 60 3>&1 1>&2 2>&3) exitstatus=$? if [ $exitstatus = 0 ]; then echo "Your password is:" $PASSWORD else echo "You chose Cancel." fi

shell-6

建立一個選單欄

當你想讓使用者選擇一個任意數量的選擇中,你可以使用選單框

語法:

whiptail –title “<menu title>” –menu “<text to show>” <height> <width> <menu height> [ <tag> <item> ] . . .

例項:

#!/bin/bash OPTION=$(whiptail --title "Test Menu Dialog" --menu "Choose your option" 15 60 4 \ "1" "Grilled Spicy Sausage" \ "2" "Grilled Halloumi Cheese" \ "3" "Charcoaled Chicken Wings" \ "4" "Fried Aubergine"  3>&1 1>&2 2>&3) exitstatus=$? if [ $exitstatus = 0 ]; then echo "Your chosen option:" $OPTION else echo "You chose Cancel." fi

shell-7

建立radiolist對話方塊

語法:

whiptail –title “<radiolist title>” –radiolist “<text to show>” <height> <width> <list height> [ <tag> <item> <status> ] . . .

例項:

#!/bin/bash DISTROS=$(whiptail --title "Test Checklist Dialog" --radiolist \ "What is the Linux distro of your choice?" 15 60 4 \ "debian" "Venerable Debian" ON \ "ubuntu" "Popular Ubuntu" OFF \ "centos" "Stable CentOS" OFF \ "mint" "Rising Star Mint" OFF 3>&1 1>&2 2>&3) exitstatus=$? if [ $exitstatus = 0 ]; then echo "The chosen distro is:" $DISTROS else echo "You chose Cancel." fi

shell-9

建立一個對話方塊

當你想讓使用者選擇一個列表中選擇多個選項的清單對話方塊是有用的,radiolist對話方塊,只允許選擇一個

語法:

whiptail –title “<checklist title>” –checklist “<text to show>” <height> <width> <list height> [ <tag> <item> <status> ] . . .

例項:

#!/bin/bash DISTROS=$(whiptail --title "Test Checklist Dialog" --checklist \ "Choose preferred Linux distros" 15 60 4 \ "debian" "Venerable Debian" ON \ "ubuntu" "Popular Ubuntu" OFF \ "centos" "Stable CentOS" ON \ "mint" "Rising Star Mint" OFF 3>&1 1>&2 2>&3) exitstatus=$? if [ $exitstatus = 0 ]; then echo "Your favorite distros are:" $DISTROS else echo "You chose Cancel." fi

shell-10

建立一個進度條

進度條是一個使用者友好的對話方塊。whiptail從標準輸入讀取一個百分數(0~100)顯示一個相應的計數

語法:

whiptail –gauge “<test to show>” <height> <width> <inital percent>

例項:

#!/bin/bash { for ((i = 0 ; i <= 100 ; i+=20)); do sleep 1 echo $i done } | whiptail --gauge "Please wait while installing" 6 60 0

shell-11

很容易在互動式shell指令碼建立有用的對話方塊了吧。後續需要寫一個互動式的shell指令碼,試著用whiptail:)