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

| 類型 | 參數 | 用途 | 關鍵參數 |
|---|---|---|---|
| 選單 | --menu | 單選項目列表 | --title --nocancel 高度 寬度 選項數 |
| 輸入框 | --inputbox | 單行文字輸入 | 預設值、--passwordbox 密碼模式 |
| 訊息框 | --msgbox | 顯示訊息(需按 OK) | 高度 寬度 |
| 是/否框 | --yesno | 確認對話框 | 返回碼 0=Yes, 1=No |
| 核取方塊 | --checklist | 多選狀態切換 | ON/OFF 初始狀態,Space 切換 |
| 單選鈕 | --radiolist | 單選 | 僅一個 ON |
| 進度條 | --gauge | 顯示進度百分比 | 需配合管道輸入數字 |
| 資訊框 | --infobox | 非阻塞顯示,腳本結束可捲動查看 | 無按鈕 |
# 將使用者選擇存入變數(關鍵!)
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 預設輸出選擇結果的通道)導向標準輸出,以便 $() 捕獲。
#!/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
# 從檔案/指令產生選單陣列
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
{
for i in {1..100}; do
sleep 0.05
echo $i
done
} | whiptail --gauge "Processing..." 6 60 0
whiptail --msgbox "$(cat /var/log/syslog)" --scrolltext 20 80
apt-get install whiptail(Debian/Ubuntu)或 yum install newt(RHEL/CentOS)3>&1 1>&2 2>&3 寫法正確trap 處理訊號,確保清理暫存檔| 特性 | whiptail | dialog |
|---|---|---|
| 預裝普及度 | Debian/Ubuntu 預裝 | RHEL/CentOS 常見 |
| 語法相容性 | 大部分參數相容 | 參數較豐富 |
| 視覺風格 | 較簡潔 | 可自訂顏色主題 |
| 建議 | 跨平台腳本優先使用 | 需進階 UI 時考慮 |
掌握 whiptail,即可為自動化運維腳本、安裝精靈、系統配置工具快速加上專業級的文字介面,大幅提升使用者體驗與操作安全性。