----------------------------本文地址: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