----------------------------本文地址:http://www.javashuo.com/article/p-sudqufrj-ge.html
----------------------------參考網址http://linuxcommand.org/lc3_adv_dialog.phpphp
1、簡介
dialog是Linux圖形化自動腳本編程的工具,實現腳本中圖形化,它是一個至關大而複雜的程序(它有近100個命令行選項),但與典型的圖形用戶界面相比,它是一個真正的輕量級。儘管如此,它仍然具備許多用戶界面技巧;
2、使用條件
dialog在多數Linux發行版上已經默認安裝若是沒有安裝,
rpm系列用如下的其中一個:
a、 yum install dialog -y
b、 rpm -vih dialog
deb系統用如下中的一個:
a、sudo apt-get install dialog
b、sudo dpkg -i dialog
3、dianlog幫助信息
dialog有不少命令選項,記憶困難,但若是理解了它,加上幫助信息,你會立刻擁有她;dialog的幫助很簡單,在終端中輸入dialog命令便可;幫助信息以下:linux
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.
4、幫助信息解析shell
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>「多是選擇該條目的狀態,這個暫時也不清楚」
具體的命令以下:bash
dialog --title "列表框" --buildlist "選擇" 0 0 15 "USER" "CPU" 0 "MEM" "MEM" 1
具體效果以下
OK~,其餘選項框也是相似的幫助信息;下面介紹部門options選項less
對話 | 選項 | 描述 | |
---|---|---|---|
構建列表 | --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 | 顯示文字信息,讓用戶有機會回答「是」或「否」。 |
5、在shell腳本中使用
dialog圖形化主要是在運維過程當中圖形自動化腳本人機交互時使用,圖形化的選擇或提示等,可操做行更強。
dialog在腳本中使用須要主要如下兩點:
a、dialog在繪製會話框自己時使用標準輸出做爲終端顯示,因此在會話框上的輸入(例如輸入到輸入框中的字符串)一般經過標準錯誤返回,這就致使了dialog獲取數據的問題,要解決該問題,須要將錯誤輸出轉換爲標準輸出文件提示符;解決該問題的方法有兩種:
1)、使用臨時存儲文件
2)使用文件描述符
b、dialog圖形化中的狀態碼,在使用dialog前先了解狀態碼;如「OK」爲0,「NO」爲1,"ESC"爲255,還能夠定義其餘狀態碼,如幫助信息等等。
6、腳本實例
本腳本實例中爲解決錯誤輸出轉換問題使用文件描述符,基本上多數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