跳轉到內容

Bash Shell 指令碼/Whiptail

來自華夏公益教科書,自由的教科書

Whiptail 是一個程式,允許 Shell 指令碼向用戶顯示 對話方塊 以供資訊目的,或以友好的方式從使用者那裡獲取輸入。Whiptail 預設包含在 Debian 中。

來自 Linux 詞典:whiptail 是使用 newt 而不是 ncurses 的“dialog”替代品。
從它的自述檔案中:whiptail 旨在與 dialog(1) 相容,但功能更少:一些對話方塊沒有實現,例如 tailbox、timebox、calendarbox 等。

嘗試使用dialog替換whiptail如果你沒有whiptail: alias whiptail='dialog'

請注意,還有其他與 dialog 相容的程式,例如 xdialog(顯示 X11 視窗)和 zenity(又名 gdialog,顯示 Gtk 視窗)。閱讀 dialog 的手冊頁應該會有所幫助。始終閱讀手冊頁或 --help 以瞭解它們與 dialog 的不同之處。

資訊框

[編輯 | 編輯原始碼]

Whiptail 中一種簡單的對話方塊型別是資訊框。這將向用戶顯示一個包含文字的對話方塊。

whiptail --title "Example Dialog" --infobox "This is an example of an info box." 8 78

在上面的示例中,--title的值顯示在對話方塊的頂部。第一個引數為--infobox是對話方塊文字,它顯示在標題下方。接下來的兩個引數指定對話方塊的高度和寬度。寬度設定為 78,因為大多數終端至少有 80 列或更多。

存在一個錯誤,導致資訊框在某些 Shell 上無法顯示。如果是這種情況,您可以將終端模擬設定為其他型別,它將起作用。

TERM=ansi whiptail --title "Example Dialog" --infobox "This is an example of an info box" 8 78
whiptail --msgbox

訊息框

[編輯 | 編輯原始碼]

訊息框與資訊框非常相似,不同之處在於它會等待使用者按下“確定”按鈕。用法與資訊框類似

whiptail --title "Example Dialog" --msgbox "This is an example of a message box. You must hit OK to continue." 8 78
whiptail --yesno

是/否框

[編輯 | 編輯原始碼]

從使用者那裡獲取輸入的最簡單方法是透過是/否框。這將顯示一個帶有兩個按鈕的對話方塊,分別標記為“是”和“否”。

# If you cannot understand this, read Bash_Shell_Scripting/Conditional_Expressions again.
if whiptail --title "Example Dialog" --yesno "This is an example of a yes/no box." 8 78; then
    echo "User selected Yes, exit status was $?."
else
    echo "User selected No, exit status was $?."
fi
whiptail --inputbox

輸入框

[編輯 | 編輯原始碼]

從使用者那裡獲取自由格式輸入的一種方法是透過輸入框。這將顯示一個帶有兩個按鈕的對話方塊,分別標記為“確定”和“取消”。

COLOR=$(whiptail --inputbox "What is your favorite Color?" 8 39 Blue --title "Example Dialog" 3>&1 1>&2 2>&3)
                                                                        # A trick to swap stdout and stderr.
# Again, you can pack this inside if, but it seems really long for some 80-col terminal users.
exitstatus=$?
if [ $exitstatus = 0 ]; then
    echo "User selected Ok and entered " $COLOR
else
    echo "User selected Cancel."
fi

echo "(Exit status was $exitstatus)"
whiptail --textbox

文字框

[編輯 | 編輯原始碼]

一個包含給定檔案內容的文字框。如果檔名超過視窗長度,則新增 --scrolltext。

echo "Welcome to Bash $BASH_VERSION" > test_textbox
#                  filename height width
whiptail --textbox test_textbox 12 80
whiptail --passwordbox

密碼框

[編輯 | 編輯原始碼]

從使用者那裡獲取隱藏密碼的一種方法是透過密碼框。這將顯示一個帶有兩個按鈕的對話方塊,分別標記為“確定”和“取消”。

PASSWORD=$(whiptail --passwordbox "please enter your secret password" 8 78 --title "password dialog" 3>&1 1>&2 2>&3)
                                                                        # A trick to swap stdout and stderr.
# Again, you can pack this inside if, but it seems really long for some 80-col terminal users.
exitstatus=$?
if [ $exitstatus == 0 ]; then
    echo "User selected Ok and entered " $PASSWORD
else
    echo "User selected Cancel."
fi

echo "(Exit status was $exitstatus)"
whiptail --menu 列表太高

無論何時您想要向用戶呈現選項列表,whiptail 都有幾種對話方塊型別可供選擇。

當您希望使用者從列表中選擇一個選項時,例如導航程式,應使用選單

whiptail --title "Menu example" --menu "Choose an option" 25 78 16 \
"<-- Back" "Return to the main menu." \
"Add User" "Add a user to the system." \
"Modify User" "Modify an existing user." \
"List Users" "List all users on the system." \
"Add Group" "Add a user group to the system." \
"Modify Group" "Modify a group and its list of members." \
"List Groups" "List all groups on the system."
whiptail --menu 列表適合

給定給 --menu 的值是

  • 描述選單的文字("選擇一個選項")
  • 對話方塊的高度(25)
  • 對話方塊的寬度(78)
  • 選單列表的高度(16)

其餘值是格式為標記 項的選單選項列表,其中標記是選項的名稱,它會列印到stderr中,當選擇時,並且是選單選項的描述。

如果您要呈現一個很長的選單,並希望充分利用可用的螢幕,您可以透過以下方式計算最佳框大小。

eval `resize`
whiptail ... $LINES $COLUMNS $(( $LINES - 8 )) ...
whiptail --checklist

複選框

[編輯 | 編輯原始碼]

在某些情況下,您需要向用戶呈現不適合放置在選單中的選項。

複選框允許使用者從列表中選擇一個或多個選項。

whiptail --title "Check list example" --checklist \
"Choose user's permissions" 20 78 4 \
"NET_OUTBOUND" "Allow connections to other hosts" ON \
"NET_INBOUND" "Allow connections from other hosts" OFF \
"LOCAL_MOUNT" "Allow mounting of local devices" OFF \
"REMOTE_MOUNT" "Allow mounting of remote devices" OFF

當用戶確認他們的選擇時,選擇的列表將列印到stderr.

whiptail --radiolist

單選框

[編輯 | 編輯原始碼]

單選框是一個對話方塊,使用者可以在其中從列表中選擇一個選項。單選框和選單的區別在於使用者選擇一個選項(在 whiptail 中使用空格鍵),然後透過按下“確定”確認該選擇。

whiptail --title "Radio list example" --radiolist \
"Choose user's permissions" 20 78 4 \
"NET_OUTBOUND" "Allow connections to other hosts" ON \
"NET_INBOUND" "Allow connections from other hosts" OFF \
"LOCAL_MOUNT" "Allow mounting of local devices" OFF \
"REMOTE_MOUNT" "Allow mounting of remote devices" OFF
whiptail --gauge

進度條

[編輯 | 編輯原始碼]

語法whiptail --gauge <文字> <高度> <寬度> [<百分比>]

也讀取百分比從 stdin

#!/bin/bash
{
    for ((i = 0 ; i <= 100 ; i+=5)); do
        sleep 0.1
        echo $i
    done
} | whiptail --gauge "Please wait while we are sleeping..." 6 50 0
華夏公益教科書