歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux下dialog圖形化工具詳解

Linux下dialog圖形化工具詳解

日期:2017/3/3 11:42:45   编辑:Linux技術
#dialog介紹
dialog是一個可以輸入圖形界面的一個工具,但他得要結合shell來一起使用,方能發揮他的功效
#通用選項
[ --title ]在對話框上面顯示標題字符串
[ --no-shadow ] 禁止陰影出現在每個對話框的底部
[ --begin <y> <x> ] 指定對話框左上角屏幕上的坐標
[ --timeout <secs> ] 超時,如果用戶在指定的時間沒有給出相應動作,就退到終端
[ --insecure ] 輸入部件的密碼時,使用*號來代表每個字符
[ --no-cancel ] 設置在輸入框、菜單和復選框中,不顯示"cancel"項
[ --backtitle <backtitle> ] 指定的backtitle字符串顯示在背景頂端
[ --clear ] 完成清屏操作,在框體顯示結束後,清除框體
[--defaultno ] 使用的默認值yes/no,使用no
[ --default-item <str> ] 設置在一份清單,表格或菜單中的默認項目。通常在框中第一項是默認
#對話框選項
[ --msgbox ] 顯示一條消息,並要求用戶選擇一個確定按鈕(消息框)
[ --infobox ] 顯示消息後,(沒有等待響應)對話框立刻返回,但不清除屏幕(信息框)
[ --yesno ] 提供一個帶有yes和no按鈕的簡單信息框 (是/否框)
[ --pause ] 用來顯示一個對話框指定的暫停時間的狀態 (暫停時間框)
[ --inputbox ] 讓用戶輸入文本(輸入框)
[ --fselect ] 提供一個路徑,讓你選擇浏覽的文件,(文件選框)
[ --passwordbox ] 顯示一個輸入框,它隱藏文本
[ --testbox ] 在帶有滾動條的文本框中顯示文本內容(文本框)
[ --editbox ] 編輯一個文件內容 (編輯框)
[ --menu ] 顯示一個列表供用戶選擇(菜單框)
[ --inputmenu ] 提代一個可供用戶編輯的菜單(可編輯的菜單框)
[ --radiolist ] 提供一個菜單項目組,只有一個項目可以選擇(單選框)
[ --checklist ] 允許你顯示一個選項列表,每個選項都可以被單獨的選擇(復選框)
[ --calendar ] 提供了一個日歷,讓你可以選擇日期(日期框)
[ --timebox ] 提供一個窗口,選擇小時、分鐘、秒(時間框)
[ --gauge ] 顯示一個表,呈現出完成的百分比,就是顯示出進度(進度框)
[ --from ] 允許你建立一個帶標簽的文本字段,並要求填寫(表單框)
dialog安裝
[root@localhost ~]# yum -y install dialog
#命令示例
1、消息框格式: dialog --msgbox <text> <height> <width>
[root@localhost ~]# dialog --title "testing" --msgbox "消息框測試"  10 20



2、信息框格式: --infobox <text> <height> <width>
[root@localhost opt]# dialog --title "testing" --infobox  "消息框測試"  10 20



3、暫停時間框格式:--pause <text> <height> <width> <seconds>
[root@localhost opt]# dialog --title "暫停時間測試" --pause "這是一個暫停期測試" 10 30 5



4、是/否框格式: dialog --yesno <text> <height> <width>
[root@localhost ~]# dialog --title "yes/no" --no-shadow --yesno "你是要刪除這個文件嗎?" 10 30



5、輸入框格式:dialog --inputbox <text> <height> <width>
[root@localhost ~]# dialog --title "輸入名字" --inputbox "請輸入你的名字" 10 30 2> /tmp/name.txt
#這裡的 2> 是將信息輸出重定向到了/tmp/name.txt文件中)



6、文件選框格式:--fselect <filepath> <height> <width>
[root@localhost ~]# dialog --title "路徑選擇" --fselect "/ " 10 50



7、密碼框格式:--passwordbox <text> <height> <width> [<init>]
[root@localhost ~]# dialog --title "password" --insecure --passwordbox "請輸入密碼" 10 30



8、文本框格式:--textbox <file> <height> <width>
[root@localhost opt]# dialog --title "tail test" --textbox "/etc/passwd" 30 80






9、編輯框格式:--editbox <file> <height> <width>
[root@localhost opt]# dialog --title "編輯一個文件" --editbox "/etc/passwd" 30 88



10、菜單框格式: --menu <text> <height> <width> <menu height> <tag1> <item1>...
[root@localhost ~]# dialog --title "Pick a choice" --menu "Choose one" 12 35 5 \
> 1 "say hello to everyone" 2 "thanks for your support" 3 "exit"



11、可編輯菜單框格式:--inputmenu <text> <height> <width> <menu height> <tag1> <item1>...
[root@localhost opt]# dialog --title "Pick a choice" --inputmenu "Choose one" 12 35 5 1 "say hello to everyone" 2 "thanks for your support" 3 "exit"



12、單選框格式:--radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...
[root@localhost opt]# dialog --backtitle "radiolist" --radiolist "Test" 20 50 10  Memory Memory_Size 1 Dsik Disk_Size 2



13、復選框格式:--checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...
dialog --backtitle "Checklist" --checklist "Test" 20 50 10 Memory Memory_Size 1 Dsik Disk_Size 2



14、顯示日歷格式:--calendar <text> <height> <width> <day> <month> <year>
#顯示當前日期
[root@localhost ~]# dialog --title "Calendar" --calendar "Date" 5 50



#顯示指定日期
[root@localhost ~]# dialog --title "Calendar" --calendar "Date" 5 50 1 1 2017



15、時間框格式:--timebox <text> <height> <width> <hour> <minute> <second>
[root@localhost opt]# dialog --title "時間選擇框" --timebox "請選擇一個時間" 5 20 1 30 20



16、進度框架格式:--gauge <text> <height> <width> [<percent>]
#固定進度顯示
[root@localhost ~]# dialog --title "installation pro" --gauge "installation" 10 30 10



#動態進度顯示
[root@localhost ~]# for i in {1..10000} ;do echo $i;done | dialog --title "installation pro" --gauge "installation" 10 30



#編輯一個gauge.sh腳本,內容如下
#!/bin/bash  
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



17、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鍵在窗口之間切換。
例子:
[root@localhost opt]# 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



綜合應用示例:
[root@localhost opt]# cat dialog.sh   
#!/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















本文出自 “wsyht90的博客” 博客,請務必保留此出處http://wsyht90.blog.51cto.com/9014030/1792957
Copyright © Linux教程網 All Rights Reserved