liunx下利用某軟件創建圖形僞界面

Liunx下的dialog是一個可以創建對話框的工具,每個對話框提供的輸出有兩種形式:1、將所有輸出到stderr,不顯示到屏幕;2、使用退出狀態碼,OK爲0,NO爲1,ESC爲255。

通用選項(common options

這個選項用來設置dialog box的背景、顏色和標題等。

--title <title>:指定將在對話框的上方顯示的標題字符串。
--colors:解讀嵌入式\ Z的對話框中的特殊文本序列,序列由下面的字符0-7bBuU等組成,恢復正常的設置使用\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>:設置在一份清單、表格或菜單中的默認項目,通常在框中的第一項是默認的。

窗體類型:

常見的對話框控件選項如下所示:

--calendar:提供了一個日曆,讓你可以選擇日期。
--checklist:允許你顯示一個選項列表,每個選項都可以被單獨的選擇(複選框)。
--from:允許建立一個帶標籤的文本字段,並要求填寫。
--fselect:提供一個路徑,讓你選擇瀏覽的文件。
--gauge:顯示一個表,呈現出完成的百分比,就是顯示出進度。
--infobox:顯示消息後,(沒有等待響應)對話框立刻返回,但不清除屏幕(信息框)。
--inputbox:讓用戶輸入文本(輸入框)。
--inputmenu:提供一個可供用戶編輯的菜單(可編輯的菜單框)。
--menu:顯示一個列表供用戶選擇(菜單框)。
--msgbox:顯示一條消息,並要求用戶選擇一個確定按鈕(消息框)。
--pause:顯示一個表格用來顯示一個指定的暫停期的狀態。
--passwordbox:顯示一個輸入框,它隱藏文本。
--passwordfrom:顯示一個來源於標籤並且隱藏的文本字段。
--radiolist:提供一個菜單項目組,只有一個項目,可以選擇(單選框)。
--tailbox:在一個滾動窗口文件中使用tail命令來顯示文本。
--tailboxbg:跟tailbox類似,但是在background模式下操作。
--textbox:在帶有滾動條的文本框中顯示文件的內容(文本框)。
--timebox:提供一個窗口,選擇小時、分鐘、秒。
--yesno:提供一個帶有yesno按鈕的簡單信息框(是/否框)。

命令示例

消息框

格式:

dialog --msgbox text height width

例子:

$ dialog --title TESTING --msgbox "this is a test" 10 20

yesno框

格式:

dialog --yesno text height width

例子:

$ dialog --title "yes/no" --no-shadow --yesno "Delete the file /tmp/canjian.txt?" 10 30

輸入框

格式:

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文件中。

密碼框

格式:

dialog --passwordbox text height width [init]

例子:

$ dialog --title "Password" --passwordbox "Please give a password for the new user:" 10 35

密碼暴露出來不安全,所以通常我們會加上一個安全選項--insecure,將每個字符用*來顯示。

$ dialog --title "Password" --insecure --passwordbox "Please give a password for the new user:" 10 30

文本框

格式:

dialog --textbox file height width

例子:

$ dialog --title "The fstab" --textbox /etc/fstab 17 40

菜單框

格式:

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"

Fselect框(文件選框)

格式:

dialog --fselect filepath height width

例子:

$ dialog --title "Pick one file" --fselect /root/ 7 40

複選框

格式:

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

顯示日曆

格式:

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 --gauge text height width  [<percent>]

例子:

固定進度顯示

$ 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  
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

from框架(表單)

格式:

dialog --form text height width formheight [ label y x item y x flen ilen ] ...

其中:flen表示field length,定義了選定字段中顯示的長度;ilen表示input-length, 定義了在外地輸入的數據允許的長度。使用up/down(或ctrl/ Nctrl/ 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

綜合應用示例

#! /bin/bashyesno() 
{
    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;


原文地址:https://segmentfault.com/a/1190000002977291#articleHeader8

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章