Linux圖形化編程dialog

----------------------------本文地址:http://blog.51cto.com/woonli/2142606
----------------------------參考網址http://linuxcommand.org/lc3_adv_dialog.php

一、簡介
dialog是Linux圖形化自動腳本編程的工具,實現腳本中圖形化,它是一個相當大而複雜的程序(它有近100個命令行選項),但與典型的圖形用戶界面相比,它是一個真正的輕量級。儘管如此,它仍然具有許多用戶界面技巧;
二、使用條件
dialog在多數Linux發行版上已經默認安裝如果沒有安裝,
rpm系列用以下的其中一個:
a、 yum install dialog -y
b、 rpm -vih dialog
deb系統用以下中的一個:
a、sudo apt-get install dialog
b、sudo dpkg -i dialog
三、dianlog幫助信息
dialog有很多命令選項,記憶困難,但如果理解了它,加上幫助信息,你會馬上擁有她;dialog的幫助很簡單,在終端中輸入dialog命令即可;幫助信息如下:

cdialog (ComeOn Dialog!) version 1.3-20160828
Copyright 2000-2015,2016 Thomas E. Dickey
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

* Display dialog boxes from shell scripts *

Usage: dialog <options> { --and-widget <options> }
where options are "common" options, followed by "box" options

Special options:
  [--create-rc "file"]
Common options:
  [--ascii-lines] [--aspect <ratio>] [--backtitle <backtitle>] [--beep]
  [--beep-after] [--begin <y> <x>] [--cancel-label <str>] [--clear]
  [--colors] [--column-separator <str>] [--cr-wrap] [--date-format <str>]
  [--default-button <str>] [--default-item <str>] [--defaultno]
  [--exit-label <str>] [--extra-button] [--extra-label <str>]
  [--help-button] [--help-label <str>] [--help-status] [--help-tags]
  [--hfile <str>] [--hline <str>] [--ignore] [--input-fd <fd>]
  [--insecure] [--item-help] [--keep-tite] [--keep-window] [--last-key]
  [--max-input <n>] [--no-cancel] [--no-collapse] [--no-cr-wrap]
  [--no-items] [--no-kill] [--no-label <str>] [--no-lines] [--no-mouse]
  [--no-nl-expand] [--no-ok] [--no-shadow] [--no-tags] [--nook]
  [--ok-label <str>] [--output-fd <fd>] [--output-separator <str>]
  [--print-maxsize] [--print-size] [--print-version] [--quoted]
  [--scrollbar] [--separate-output] [--separate-widget <str>] [--shadow]
  [--single-quoted] [--size-err] [--sleep <secs>] [--stderr] [--stdout]
  [--tab-correct] [--tab-len <n>] [--time-format <str>] [--timeout <secs>]
  [--title <title>] [--trace <file>] [--trim] [--version] [--visit-items]
  [--week-start <str>] [--yes-label <str>]
Box options:
  --buildlist    <text> <height> <width> <list-height> <tag1> <item1> <status1>...
  --calendar     <text> <height> <width> <day> <month> <year>
  --checklist    <text> <height> <width> <list height> <tag1> <item1> <status1>...
  --dselect      <directory> <height> <width>
  --editbox      <file> <height> <width>
  --form         <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>...
  --fselect      <filepath> <height> <width>
  --gauge        <text> <height> <width> [<percent>]
  --infobox      <text> <height> <width>
  --inputbox     <text> <height> <width> [<init>]
  --inputmenu    <text> <height> <width> <menu height> <tag1> <item1>...
  --menu         <text> <height> <width> <menu height> <tag1> <item1>...
  --mixedform    <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> <itype>...
  --mixedgauge   <text> <height> <width> <percent> <tag1> <item1>...
  --msgbox       <text> <height> <width>
  --passwordbox  <text> <height> <width> [<init>]
  --passwordform <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>...
  --pause        <text> <height> <width> <seconds>
  --prgbox       <text> <command> <height> <width>
  --programbox   <text> <height> <width>
  --progressbox  <text> <height> <width>
  --radiolist    <text> <height> <width> <list height> <tag1> <item1> <status1>...
  --rangebox     <text> <height> <width> <min-value> <max-value> <default-value>
  --tailbox      <file> <height> <width>
  --tailboxbg    <file> <height> <width>
  --textbox      <file> <height> <width>
  --timebox      <text> <height> <width> <hour> <minute> <second>
  --treeview     <text> <height> <width> <list-height> <tag1> <item1> <status1> <depth1>...
  --yesno        <text> <height> <width>

Auto-size with height and width = 0. Maximize with height and width = -1.
Global-auto-size if also menu_height/list_height = 0.

四、幫助信息解析

Usage: dialog <options> { --and-widget <options> }

dialog 命令後是common options,即通用選項,是圖形化框的一些個性化配置信息,依據是否需要可以省略;而--and-widget 是各類圖形化部件,後邊重點介紹,部件的options主要是針對部件的高、寬、內容、位置等參數的設置;
如幫助信息中的

  --buildlist    <text> <height> <width> <list-height> <tag1> <item1> <status1>...

是爲了構建並排顯示兩個列表。左側的列表包含未選擇的項目,右側所選項目的列表。用戶可以將項目從一個列表移動到另一個列表。
在--buildlist的options中,

<text> <height> <width> <list-height> <tag1> <item1> <status1>...

"<text>"是自定義框體名稱,<height> <width>是框體的高和寬,默認值爲0,默認時自適應大小,<list-height>是列表選擇框的高度,<tag1> 是列表選擇框的提示或者標題等;<item1>是列表的選項; <status1>“可能是選擇該條目的狀態,這個暫時也不清楚”
具體的命令如下:

dialog --title "列表框" --buildlist "選擇" 0 0 15 "USER" "CPU" 0 "MEM" "MEM" 1

具體效果如下
Linux圖形化編程dialog
OK~,其他選項框也是類似的幫助信息;下面介紹部門options選項

  1. common options
    [--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>] 設置在一份清單,表格或菜單中的默認項目。通常在框中的第一項是默認。
  2. Windows options
對話 選項 描述
構建列表 --buildlist 並排顯示兩個列表。左側的列表包含未選擇的項目,右側所選項目的列表。用戶可以將項目從一個列表移動到另一個列表。
日曆 --calendar 顯示日曆並允許用戶選擇日期。
清單 --checklist 顯示選項列表,並允許用戶選擇一個或多個項目。
目錄選擇 --dselect 顯示目錄選擇對話框。
編輯框 --editbox 顯示基本文本文件編輯器。
形成 --form 允許用戶在多個字段中輸入文本。
文件選擇 --fselect 文件選擇對話框。
測量 --gauge 顯示進度指示器,顯示完成百分比。
信息框 --infobox 顯示消息(帶有可選的定時暫停)並終止。
輸入框 --inputbox 提示用戶輸入/編輯文本字段。
菜單框 --menubox 顯示選項列表。
消息框 --msgbox 顯示文本消息並等待用戶響應。
密碼箱 --passwordbox 與輸入框類似,但隱藏用戶的條目。
暫停 --pause 顯示文本消息和倒數計時器。當計時器用完或用戶按下“確定”或“取消”按鈕時,對話框將終止。
程序框 --programbox 顯示管道命令的輸出。命令完成後,對話框將等待用戶按下OK按鈕。
進度框 --progressbox 類似於程序框,除了對話框在管道命令完成時終止,而不是等待用戶按OK。
電臺名單 --radiolist 顯示選項列表,並允許用戶選擇單個項目。之前選擇的任何項目都將被取消選中。
範圍框 --rangebox 允許用戶使用基於鍵盤的滑塊在指定範圍內選擇數值。
尾箱 --tailbox 顯示具有實時更新的文本文件。像命令一樣工作tail -f。
文本框 --textbox 簡單的文本文件查看器。支持許多相同的鍵盤命令less。
時間盒 --timebox 用於輸入時間的對話框。
樹視圖 --treeview 顯示樹形層次結構中的項目列表。
是/否盒子 --yesno 顯示文字信息,讓用戶有機會回答“是”或“否”。

五、在shell腳本中使用
dialog圖形化主要是在運維過程中圖形自動化腳本人機交互時使用,圖形化的選擇或提示等,可操作行更強。
dialog在腳本中使用需要主要以下兩點:
a、dialog在繪製會話框本身時使用標準輸出作爲終端顯示,所以在會話框上的輸入(例如輸入到輸入框中的字符串)通常通過標準錯誤返回,這就導致了dialog獲取數據的問題,要解決該問題,需要將錯誤輸出轉換爲標準輸出文件提示符;解決該問題的方法有兩種:
1)、使用臨時存儲文件
2)使用文件描述符
b、dialog圖形化中的狀態碼,在使用dialog前先了解狀態碼;如“OK”爲0,“NO”爲1,"ESC"爲255,還可以定義其他狀態碼,如幫助信息等等。

六、腳本實例
本腳本實例中爲解決錯誤輸出轉換問題使用文件描述符,基本上多數dialog腳本中都使用文件描述符;格式爲
exec 3>&1
dialog --widdget options 2>&1 1>&3
exec 3>&1;
該命令包括將描述符2(stderr)重定向爲描述符1的副本,最後,通過複製包含備份副本的描述符3,將描述符1恢復爲其原始值。可能不會立即顯示的是爲什麼需要最後一次重定向。在子shell中,標準輸出(描述符1)不指向控制終端。相反,它指向一個將其內容傳遞給變量的管道result。由於dialog需要標準輸出指向終端以便它可以顯示輸入框,我們必須將標準錯誤重定向到標準輸出(以便輸出dialog結束於result 變量),然後將標準輸出重定向回控制終端。
腳本實例:

#!/bin/bash
#Dialog use case
#Author by woon

## Define the dialog exit status codes
#可以不定義退出碼。直接使用值也行。
: ${DIALOG_OK=0}
: ${DIALOG_CANCEL=1}
: ${DIALOG_HELP=2}
: ${DIALOG_EXTRA=3}
: ${DIALOG_ITEM_HELP=4}
: ${DIALOG_ESC=255}

#定義msgbox函數
display_box() {
dialog --title "$1" \
    --no-collapse \
    --msgbox "$res" 0 0
    }
while true;do
    #將標準輸出複製一份到文件描述符3,  
    exec 3>&1
    selection=$(dialog --title "系統監測" \
        --clear \
        --ok-label "Submit" \
        --cancel-label "Exit" \
        --menu "選擇監測項:" 0 0 10 \
        "1" "當前用戶佔用資源情況" \
        "2" "當前磁盤使用情況" \
        "3" "CPU相關信息" \
        "4" "MEM相關信息" \
        2>&1 1>&3   #將錯誤輸出轉換爲標準輸出
        )

    exit_status=$?
    exec 3>&- #關閉文件描述符3
    case $exit_status in 
        $DIALOG_CANCEL)
        clear
        echo "程序終止"
        exit
        ;;
    $DIALOG_ESC)
    clear 
    echo "程序終止"
    exit 1
    ;;
esac
case $selection in 
    0)
        clear
        echo "程序終止"
        ;;
    1)
        pcpu=$(ps -eo user,pcpu,pmem | awk -v user=$USER '$1==user{print $0}'|awk '{pcpu+=$2}END{print pcpu}')
        pmem=$(ps -eo user,pcpu,pmem | awk -v user=$USER '$1==user{print $0}'|awk '{pcpu+=$2}END{print pcpu}')
        res="當前用戶佔用CPU:"$pcpu"\n當前用戶佔用內存:"$pmem
        display_box "當前用戶佔用資源情況"
        ;;
    2)
        res=$(df -h)
        display_box "當前磁盤使用情況"
        ;;
    3)
        res=$(dmesg | grep CPU | awk -F "] " '{print $2}')
        display_box "CPU硬件信息"
        ;;
    4)
        res=$(head -10 /proc/meminfo )
        display_box "系統內存信息"
        ;;
esac
done
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章