萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 使用 whiptail 建立互動式終端機對話框完整教學

whiptail 是新版 Debian/Ubuntu 系統預裝的 newt 程式庫前端工具,能在純文字終端機中繪製選單、輸入框、訊息框、進度條等圖形化介面,是 Shell 腳本開發互動式選單的利器。本文將從基礎語法到完整 Email 管理系統範例,完整解析 whiptail 實戰技巧。

一、whiptail 核心對話框類型與參數

whiptail 對話框示例

類型參數用途關鍵參數
選單--menu單選項目列表--title --nocancel 高度 寬度 選項數
輸入框--inputbox單行文字輸入預設值、--passwordbox 密碼模式
訊息框--msgbox顯示訊息(需按 OK)高度 寬度
是/否框--yesno確認對話框返回碼 0=Yes, 1=No
核取方塊--checklist多選狀態切換ON/OFF 初始狀態,Space 切換
單選鈕--radiolist單選僅一個 ON
進度條--gauge顯示進度百分比需配合管道輸入數字
資訊框--infobox非阻塞顯示,腳本結束可捲動查看無按鈕

關鍵 I/O 重導向技巧

# 將使用者選擇存入變數(關鍵!)
OPTION=$(whiptail --title "Menu" --menu "Choose:" 15 60 4 \
"1" "Option 1" "2" "Option 2" 3>&1 1>&2 2>&3)

3>&1 1>&2 2>&3 將標準錯誤輸出(whiptail 預設輸出選擇結果的通道)導向標準輸出,以便 $() 捕獲。

二、完整實戰範例:Email 管理系統

#!/bin/bash
# Email Manager using whiptail
# 需配合 /etc/postfix/virtual_mailbox_maps 及 mailadd.sh/maildel.sh 使用

trap "" 2  # 忽略 Ctrl+C

while true; do
    OPTION=$(whiptail --title "Email Manager" --nocancel --menu "Choose your option" 15 60 4 \
        "1" "Add Email User" \
        "2" "Delete Email User" \
        "3" "List Email User" \
        "4" "EXIT" 3>&1 1>&2 2>&3)

    case $OPTION in
        1)  # 新增
            EmailAddress=$(whiptail --title "Add Email" --inputbox "Enter email to add:" 10 60 "@example.com" 3>&1 1>&2 2>&3)
            exitstatus=$?
            if [ $exitstatus -eq 0 ]; then
                if grep -q "$EmailAddress" /etc/postfix/virtual_mailbox_maps; then
                    whiptail --msgbox "Email $EmailAddress already exists!" 10 40
                elif whiptail --title "Confirm" --yesno "Add $EmailAddress?" 10 60; then
                    /etc/postfix/mailadd.sh "$EmailAddress"
                    whiptail --msgbox "Added: $EmailAddress" 10 40
                fi
            else
                whiptail --msgbox "Cancelled." 10 40
            fi
            ;;
        2)  # 刪除
            EmailAddress=$(whiptail --title "Delete Email" --inputbox "Enter email to delete:" 10 60 "@example.com" 3>&1 1>&2 2>&3)
            exitstatus=$?
            if [ $exitstatus -eq 0 ]; then
                if ! grep -q "$EmailAddress" /etc/postfix/virtual_mailbox_maps; then
                    whiptail --msgbox "Email $EmailAddress not found!" 10 40
                elif whiptail --title "Confirm" --yesno "Delete $EmailAddress?" 10 60; then
                    /etc/postfix/maildel.sh "$EmailAddress"
                    whiptail --msgbox "Deleted: $EmailAddress" 10 40
                fi
            else
                whiptail --msgbox "Cancelled." 10 40
            fi
            ;;
        3)  # 列出
            EmailList=$(awk '{print $1}' /etc/postfix/virtual_mailbox_maps)
            whiptail --msgbox "Email Users:\n$EmailList" --scrolltext 20 60
            ;;
        4)  # 離開
            echo "EXIT"
            break
            ;;
    esac
done

trap 2  # 恢復 Ctrl+C

三、進階技巧與常見坑點

1. 動態產生選單項目

# 從檔案/指令產生選單陣列
mapfile -t USERS < /etc/postfix/virtual_mailbox_maps
MENU_ARGS=()
for i in "${!USERS[@]}"; do
    MENU_ARGS+=("$((i+1))" "${USERS[i]}")
done
whiptail --menu "Users" 20 60 10 "${MENU_ARGS[@]}" 3>&1 1>&2 2>&3

2. 進度條實作

{
    for i in {1..100}; do
        sleep 0.05
        echo $i
    done
} | whiptail --gauge "Processing..." 6 60 0

3. 多行文字捲動

whiptail --msgbox "$(cat /var/log/syslog)" --scrolltext 20 80

4. 常見錯誤排查

  • 找不到指令:安裝 apt-get install whiptail(Debian/Ubuntu)或 yum install newt(RHEL/CentOS)
  • 終端機太小:調整高度/寬度參數,最小建議 10x40
  • 變數捕獲失敗:確認 3>&1 1>&2 2>&3 寫法正確
  • 腳本中途被殺:使用 trap 處理訊號,確保清理暫存檔

四、whiptail vs dialog

特性whiptaildialog
預裝普及度Debian/Ubuntu 預裝RHEL/CentOS 常見
語法相容性大部分參數相容參數較豐富
視覺風格較簡潔可自訂顏色主題
建議跨平台腳本優先使用需進階 UI 時考慮

掌握 whiptail,即可為自動化運維腳本、安裝精靈、系統配置工具快速加上專業級的文字介面,大幅提升使用者體驗與操作安全性。

copyright © 萬盛學電腦網 all rights reserved