liunx 下的dialog 工具是一個可以和shell腳本配合使用的文本界面下的創建對話框的工具。
每個對話框提供的輸出有兩種形式:
1. 將所有輸出到stderr 輸出,不顯示到屏幕。
2. 使用退出狀態碼,“OK”為0,“NO”為1,"ESC"為255
[--title <title>] 指定將在對話框的上方顯示的標題字符串
[--colors] 解讀嵌入式“\ Z”的對話框中的特殊文本序列,序列由下面的字符 0-7, b B, u, U等,恢復正常的設置使用“\Zn”。
[--no-shadow] 禁止陰影出現在每個對話框的底部
[--shadow] 應該是出現陰影效果
[--insecure] 輸入部件的密碼時,明文顯示不安全,使用星號來代表每個字符
[--no-cancel] 設置在輸入框,菜單,和復選框中,不顯示“cancel”項
[--clear] 完成清屏操作。在框體顯示結束后,清除框體。這個參數只能單獨使用,不能和別的參數聯合使用。
[--ok-label <str>] 覆蓋使用“OK”按鈕的標簽,換做其他字符。
[--cancel-label <str>] 功能同上
[--backtitle <backtitle>] 指定的backtitle字符串顯示在背景頂端。
[--begin <y> <x>] 指定對話框左上角在屏幕的上的做坐標
[--timeout <secs>] 超時(返回的錯誤代碼),如果用戶在指定的時間內沒有給出相應動作,就按超時處理
[--defaultno] 使的是默認值 yes/no,使用no
[--sleep <secs>]
[--stderr] 以標准錯誤方式輸出
[--stdout] 以標准方式輸出
[--default-item <str>] 設置在一份清單,表格或菜單中的默認項目。通常在框中的第一項是默認
窗體類型:
使用命令dialog也可以直接查看具體參數
常見的對話框控件選項有:
[ –calendar ] 提供了一個日歷,讓你可以選擇日期
[ –checklist ] 允許你顯示一個選項列表,每個選項都可以被單獨的選擇 (復選框)
[ –from ] 允許您建立一個帶標簽的文本字段,並要求填寫
[ –fselect ] 提供一個路徑,讓你選擇瀏覽的文件
[ –gauge ] 顯示一個表,呈現出完成的百分比,就是顯示出進度。
[ –infobox ] 顯示消息后,(沒有等待響應)對話框立刻返回,但不清除屏幕 (信息框)
[ –inputbox ] 讓用戶輸入文本 (輸入框 )
[ –inputmenu ] 提供一個可供用戶編輯的菜單 (可編輯的菜單框)
[ –menu ] 顯示一個列表供用戶選擇 (菜單框)
[ –msgbox ] 顯示一條消息,並要求用戶選擇一個確定按鈕 (消息框 )
[ –pause ] 顯示一個表格用來顯示一個指定的暫停期的狀態
[ –passwordbox ] 顯示一個輸入框,它隱藏文本
[ –passwordfrom ] 顯示一個來源於標簽並且隱藏的文本字段
[ –radiolist ] 提供一個菜單項目組,只有一個項目,可以選擇 (單選框 )
[ –tailbox ] 在一個滾動窗口文件中使用tail命令來顯示文本
[ –tailboxbg] 跟tailbox類似,但是在background模式下操作
[ –textbox ] 在帶有滾動條的文本框中顯示文件的內容 (文本框)
[ –timebox ] 提供一個窗口,選擇小時,分鍾,秒
[ –yesno ] 提供一個帶有yes和no按鈕的簡單信息框 (是/否框)
如果沒有此包請先安裝
yum -y install dialog
1. 消息框
例子:
例子:
(這里的2>是將錯誤信息輸出重定向到了/tmp/name.txt文件中)
4.密碼框
這樣我們的密碼就暴露出來了,是不是很不安全,所以通常我們會加上一個安全選項
8.復選框
格式:dialog --checklist "Test" height width menu-height tag1 item1 tag2 item2 …
例子:
# dialog --backtitle "Checklist" --checklist "Test" 20 50 10 Memory Memory_Size 1 Dsik Disk_Size 2
9 .顯示日歷
格式:dialog --calendar "Date" height width day month year
例子:
#顯示當前日期
# dialog --title "Calendar" --calendar "Date" 5 50
# dialog --title "Calendar" --calendar "Date" 5 50 1 2 2013
#固定進度顯示
# dialog --title "installation pro" --gauge "installation" 10 30 10
#實時動度進度
# for i in {1..100} ;do echo $i;done | dialog --title "installation pro" --gauge "installation" 10 30
編輯一個gauge.sh 的腳本
#!/bin/bash
# vim gauge.sh
declare -i PERCENT=0
(
for I in /etc/*;do
if [ $PERCENT -le 100 ];then
cp -r $I /tmp/test 2> /dev/null
echo "XXX"
echo "Copy the file $I ..."
echo "XXX"
echo $PERCENT
fi
let PERCENT+=1
sleep 0.1
done
) | dialog --title "coping" --gauge "starting to copy files..." 6 50 0
11.from 框架(表單)
#!/bin/bash
yesno() {
dialog --title "First screen" --backtitle "Test Program" --clear --yesno \
"Start this test program or not ? \nThis decesion have to make by you. " 16 51
# yes is 0, no is 1 , esc is 255
result=$?
if [ $result -eq 1 ] ; then
exit 1;
elif [ $result -eq 255 ]; then
exit 255;
fi
username
}
username() {
cat /dev/null >/tmp/test.username
dialog --title "Second screen" --backtitle "Test Program" --clear --inputbox \
"Please input your username (default: hello) " 16 51 "hello" 2>/tmp/test.username
result=$?
if [ $result -eq 1 ] ; then
yesno
elif [ $result -eq 255 ]; then
exit 255;
fi
password
}
password() {
cat /dev/null >/tmp/test.password
dialog --insecure --title "Third screen" --backtitle "Test Program" --clear --passwordbox \
"Please input your password (default: 123456) " 16 51 "123456" 2>/tmp/test.password
result=$?
if [ $result -eq 1 ] ; then
username
elif [ $result -eq 255 ]; then
exit 255;
fi
occupation
}
occupation() {
cat /dev/null >/tmp/test.occupation
dialog --title "Forth screen" --backtitle "Test Program" --clear --menu \
"Please choose your occupation: (default: IT)" 16 51 3 \
IT "The worst occupation" \
CEO "The best occupation" \
Teacher "Not the best or worst" 2>/tmp/test.occupation
result=$?
if [ $result -eq 1 ] ; then
password
elif [ $result -eq 255 ]; then
exit 255;
fi
finish
}
finish() {
dialog --title "Fifth screen" --backtitle "Test Program" --clear --msgbox \
"Congratulations! The test program has finished!\n Username: $(cat /tmp/test.username)\n Password: $(cat /tmp/test.password)\n Occupation: $(cat /tmp/test.occupation)" 16 51
result=$?
if [ $result -eq 1 ] ; then
occupation
elif [ $result -eq 255 ]; then
exit 255;
fi
}
yesno
