shell dialog详细

转载自:http://www.ttlsa.com/linux-command/linux-dialog-shell/
最近使用dialog写图形自动化shell脚本, 功能很强大,功能不是非常多但是足够用。想写一篇linux下dialog的使用方法,虽然命令不多,但是写起来也需要下很大功夫,而且不一定写得更好,在网上发现一篇linux shell图形化脚本文件,于是转过来了.

liunx 下的dialog 工具是一个可以和shell脚本配合使用的文本界面下的创建对话框的工具。
每个对话框提供的输出有两种形式:

  1. 将所有输出到stderr 输出,不显示到屏幕。
  2. 使用退出状态码,“OK”为0,“NO”为1,"ESC"为255

通用选项 common options:(这个选项来设置dialog box的背景,颜色和 标题等)**[--title <title>] 指定将在对话框的上方显示的标题字符串
[--colors] ** 解读嵌入式“\ Z”的对话框中的特殊文本序列,序列由下面的字符 0-7, b B, u, U等,恢复正常的设置使用“\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 ****] ** 提供一个带有yes和no按钮的简单信息框 (是/否框)

如果没有此包请先安装
yum -y install dialog

命令示例: 1.****消息框 格式:dialog - -msgbox text height width

例子:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 15px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

dialog - -title TESTING - -msgbox “this is a test “ 10 20

2.yesno框

格式:dialog –yesno text height width
例子:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 30px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

dialog --title "yes/no" --no-shadow --yesno \

"Delete the file /tmp/chensiyao.txt?" 10 30

3.输入框

格式:dialog --inputbox text height width
例子:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 45px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

dialog --title "Input your name" \

--inputbox "Please input your name:" 10 30 2> /tmp/name.txt

(这里的2>是将错误信息输出重定向到了/tmp/name.txt文件中)

4.密码框

格式:

dialog --passwordbox text height width [init]

例子:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 30px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

dialog --title "Password" --passwordbox \

"Please give a password for the new user:" 10 35

这样我们的密码就暴露出来了,是不是很不安全,所以通常我们会加上一个安全选项

--insecure 将每个字符用*来显示出来

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 30px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

dialog --title "Password" --insecure \

--passwordbox "Please give a password for the new user:" 10 30

5.文本框

格式:dialog --textbox file height width

例子:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 15px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

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

6.菜单框

格式:dialog --menu text height width menu-height tag1 item1 tag2 item2 …

例子:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 30px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

dialog --title "Pick a choice" --menu "Choose one" 12 35 5 \

1 "say hello to everyone" 2 "thanks for your support" 3 "exit"

7.Fselect框(****文件选框)

格式:dialog --fselect filepath height width

例子:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 15px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

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

8.复选框 格式:dialog --checklist "Test" height width menu-height tag1 item1 tag2 item2 …
例子:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 30px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

dialog --backtitle "Checklist" --checklist "Test" 20 50 10 \

Memory Memory_Size 1 Dsik Disk_Size 2<b></b>

9****.显示日历
格式:dialog --calendar "Date" height width day month year
例子:

显示当前日期

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 15px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

dialog --title "Calendar" --calendar "Date" 5 50

显示指定日期

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 15px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

dialog --title "Calendar" --calendar "Date" 5 50 1 2 2013

10.进度框架

格式:dialog --gauge text height width [<percent>]

例子:

固定进度显示

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 15px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

dialog --title "installation pro" --gauge "installation" 10 30 10

实时动度进度

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 30px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

for i in {1..100} ;do echo $i;done | dialog --title \

"installation pro" --gauge "installation" 10 30

|

编辑到脚本中

编辑一个gauge.sh 的脚本

内容如下:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 240px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

!/bin/bash

vim gauge.sh

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

|

bash gauge.sh (执行脚本的时候注意修改权限)

11.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键在窗口之间切换。

例子:

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

综合应用示例:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 12px !important; font-family: Monaco, MonacoRegular, "Courier New", monospace !important; color: rgb(0, 0, 0); line-height: 15px !important; background: rgb(255, 255, 255); width: 699.391px; overflow: auto; padding: 0px 5px; margin: 0px; height: 951px; position: absolute; opacity: 0; border: 0px; border-radius: 0px; box-shadow: none; white-space: pre; overflow-wrap: normal; resize: none; tab-size: 4;"></textarea>

!/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

|

[图片上传失败...(image-52dbd9-1551407860067)]

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,695评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,569评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,130评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,648评论 1 297
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,655评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,268评论 1 309
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,835评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,740评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,286评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,375评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,505评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,185评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,873评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,357评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,466评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,921评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,515评论 2 359

推荐阅读更多精彩内容

  • 幸运在不期而至的时候降临。 没想到哀嚎一片中,被我抽到了深马的签,感觉兴奋程度不亚于中了乐透。 原本只是作为两手准...
    057Bonnie阅读 123评论 0 1
  • 今天一早,儿因为看到一个好友的空间里发表了一条消息而大发感慨,消息内容是这样的:为什么有那些在意成绩的人呢...
    大爱无疆杨青阅读 222评论 0 6
  • 玉米的基因外显子的长度分布情况 外显子的平均长度为:283.9872外显子长度的中位数:142外显子的总长度:79...
    灵动的小猪阅读 869评论 0 0
  • 从今天开始,做个快乐的人。 不做情绪的奴隶,不抱怨,不逃避。 直面内心,诚实正直,面带微笑,心怀感恩。 正知正见正...
    昆书阅读 691评论 6 6
  • 我的职业是培训师。 行业的特殊性,我必须经常出差。一方面出差距离相对比较近,另一方面公司为了节约成本,所以,我不是...
    明天需要铭记阅读 427评论 0 2