純文字腳本也能有 menu、輸入框與確認視窗,關鍵工具是 whiptail。以下以一個電子郵件帳號管理腳本為例,說明各元件的使用方式。

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
--title 是標題;--nocancel 只顯示 OK 不顯示取消;--menu 後的三個數字分別是高度、寬度與項目數;最後那串 3>&1 1>&2 2>&3 是關鍵,用來把選擇結果擷取進變數 OPTION。
--inputbox "What is your EmailAddress?" 10 60 @shenxu.com
會產生一個可輸入的提示框,@shenxu.com 是預設值。
whiptail --msgbox "You chose Cancel." 10 40
--scrolltext 20 40 則讓多行內容可以上下捲動。
whiptail --checklist "choose" 15 60 2 "1" "aa" ON "2" "bb" ON
比 menu 多一個 ON/OFF 表示預設是否勾選,以空白鍵切換,可多選;--radiolist 則只能單選。
--passwordbox 的輸入方式同 inputbox,但會以星號遮蔽;--gauge 可顯示進度條,搭配迴圈輸出數字即可:
{ for n in `seq 100`; do sleep 1; echo $n; done; } | whiptail --gauge "Please wait while installing" 6 60 0
把這些元件組進 while 迴圈與 case 分支,就能做出一個具備選單、表單與回饋的完整互動腳本。記得在開頭加上 trap '' 2,避免使用者按 Ctrl+c 時畫面殘留。