linux dialog詳解(圖形化shell)


  liunx 下的dialog 工具是一個可以和shell腳本配合使用的文本界面下的創建對話框的工具。
每個對話框提供的輸出有兩種形式:
   1.  將所有輸出到stderr 輸出,不顯示到屏幕。
   2.  使用退出狀態碼,“OK”為0,“NO”為1,"ESC"為255

 
通用選項 common options:(這個選項來設置dialog box的背景,顏色和 標題等)

[--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. 消息框
格式:dialog  - -msgbox  text   height  width
例子:
# dialog    --title   TESTING   --msgbox  "this   is  a test" 10  20

linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
2.yesno框
格式:dialog –yesno  text  height  width
例子:
# dialog --title "yes/no" --no-shadow --yesno "Delete the file /tmp/chensiyao.txt?" 10 30

linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
3.輸入框
格式:dialog --inputbox text height width
例子:
# dialog --title "Input your name"  --inputbox "Please input your name:" 10 30  2> /tmp/name.txt   
(這里的2>是將錯誤信息輸出重定向到了/tmp/name.txt文件中)

linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分

4.密碼框
格式:
dialog  --passwordbox text height width [init]
例子:
dialog --title "Password"  --passwordbox "Please give a password for the new user:" 10 35
 
linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
這樣我們的密碼就暴露出來了,是不是很不安全,所以通常我們會加上一個安全選項
--insecure   將每個字符用*來顯示出來
#  dialog  --title  "Password"  --insecure  --passwordbox  "Please  give  a  password  for the  new  user:"  10  30

linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
5.文本框
格式:dialog --textbox file height width
例子:
#  dialog --title "The fstab" --textbox /etc/fstab  17 40

linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分

6.菜單框
格式:dialog --menu text height width  menu-height tag1 item1 tag2 item2 …
例子:
#  dialog --title "Pick a choice" --menu "Choose one" 12 35 5 1 "say hello to everyone" 2 "thanks for your support" 3 "exit"

linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
7.Fselect框( 文件選框)
格式:dialog --fselect filepath height width
例子:
dialog --title "Pick one file" --fselect /root/ 7 40

linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分

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

linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分

9 .顯示日歷
格式:dialog --calendar "Date" height width day month year
例子:
#顯示當前日期
#  dialog --title "Calendar" --calendar "Date" 5 50
linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 #顯示指定日期
dialog --title "Calendar" --calendar "Date" 5 50 1 2 2013
linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
10.進度框架
格式:dialog --gauge text height width  [<percent>]
例子:
#固定進度顯示
dialog --title "installation pro" --gauge "installation" 10 30 10 
linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
#實時動度進度
#  for i in {1..100} ;do echo $i;done | dialog --title "installation pro" --gauge "installation" 10 30
linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
#編輯到腳本中
編輯一個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

 
#bash  gauge.sh  (執行腳本的時候注意修改權限)
 

 



11.from 框架(表單)
格式:dialog --form text height width formheight [ label y x item y x flen ilen ] ...
其中
flen 表示field length,定義了:選定字段中顯示的長度
ilen 表示 input-length, 定義了:在外地輸入的數據允許的長度
使用up/down(或ctrl/ N,ctrl/ P)在使用領域之間移動。使用tab鍵在窗口之間切換。
例子:
# dialog --title "Add a user" --form "Please input the infomation of new user:" 12 40 4  \
  "Username:" 1  1 "" 1  15  15  0  \
  "Full name:" 2  1 "" 2  15  15  0  \
  "Home Dir:" 3  1 "" 3  15  15  0  \
  "Shell:"    4   1 "" 4  15  15  0     

linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
綜合應用示例:

#!/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


linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 
linux shell圖形界面dialog詳解 - 天空下的緣分 - 天空下的緣分
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM