shell & dialog
最近使用dialog写图形自动化shell脚本, 功能很强大,功能不是非常多但是足够用。想写一篇linux下dialog的使用方法,虽然命令不多,但是写起来也需要下很大功夫,而且不一定写得更好,在网上发现一篇linux shell图形化脚本文件,于是转过来了.
liunx 下的dialog 工具是一个可以和shell脚本配合使用的文本界面下的创建对话框的工具。
每个对话框提供的输出有两种形式:
1. 将所有输出到stderr 输出,不显示到屏幕。
2. 使用退出状态码,“OK”为0,“NO”为1,"ESC"为255
[--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>] 设置在一份清单,表格或菜单中的默认项目。通常在框中的第一项是默认
窗体类型:
[ --yesno ] 提供一个带有yes和no按钮的简单信息框 (是/否框)
如果没有此包请先安装
yum -y install dialog
命令示例:
1.消息框 格式:dialog - -msgbox text height width
|
1
|
#dialog - -title TESTING - -msgbox “this is a test “ 10 20
|

例子:
|
1
2
|
#dialog --title "yes/no" --no-shadow --yesno \
"Delete the file /tmp/chensiyao.txt?" 10 30
|

例子:
|
1
2
3
|
# dialog --title "Input your name" \
--inputbox "Please input your name:" 10 30 2> /tmp/name.txt
#(这里的2>是将错误信息输出重定向到了/tmp/name.txt文件中)
|
4.密码框|
1
2
|
# dialog --title "Password" --passwordbox \
"Please give a password for the new user:" 10 35
|

这样我们的密码就暴露出来了,是不是很不安全,所以通常我们会加上一个安全选项
|
1
2
|
# dialog --title "Password" --insecure \
--passwordbox "Please give a password for the new user:" 10 30
|

|
1
|
#dialog --title "The fstab" --textbox /etc/fstab 17 40
|
|
1
2
|
#dialog --title "Pick a choice" --menu "Choose one" 12 35 5 \
1 "say hello to everyone" 2 "thanks for your support" 3 "exit"
|

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

8.复选框
格式:dialog --checklist "Test" height width menu-height tag1 item1 tag2 item2 …
例子:
|
1
2
|
# 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
例子:
#显示当前日期
|
1
|
# dialog --title "Calendar" --calendar "Date" 5 50
|

#显示指定日期
|
1
|
# dialog --title "Calendar" --calendar "Date" 5 50 1 2 2013
|

#固定进度显示
|
1
|
#dialog --title "installation pro" --gauge "installation" 10 30 10
|

#实时动度进度
|
1
2
|
#for i in {1..100} ;do echo $i;done | dialog --title \
"installation pro" --gauge "installation" 10 30
|

编辑一个gauge.sh 的脚本
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#!/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
|


综合应用示例:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#!/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
|





shell & dialog的更多相关文章
- 关于JFace的自定义对话框(Dialog类)
仅仅是使用MessageDialog,InputDialog等JFace中现成的对话框类是无法满足实际项目开发需要的. 很多时候都需要自己定制对话框,自定义对话框只要在Dialog类的基础上作扩展就行 ...
- 关于JFace中的进度条对话框(ProgressMonitorDialog类)
在Windows操作系统中,最常用的进度条对话框就是文件复制时的弹出框,如果想让用户愉快的使用你开发 的软件,那么在执行某个较长时间的操作时候,就应该弹出一个进度条提示框,告诉用户程序正在做什么. 做 ...
- 关于JFace中的对话框MessageDialog类等其它类型对话框
对话框是软件系统中最常用到的界面,对话框无处不在,从界面结构来说,对话框主要是由主体的界面组件和底部窗体按钮组成. 之前的例子中已经频繁的使用到了MessageDialog.openInformati ...
- 简介SWT Jface
可以使用标准窗口小部件工具箱(Standard Widget Toolkit,SWT)和 JFace 库来开发用于 Eclipse 环境的图形用户界面,而且还可以将它们用于开发单独的 GUI 本机应用 ...
- 3java面试题 传智 发的 有用
第一章内容介绍 20 第二章JavaSE基础 21 一.Java面向对象 21 1. 面向对象都有哪些特性以及你对这些特性的理解 21 2. 访问权限修饰符public.private.protect ...
- linux dialog详解(图形化shell)
liunx 下的dialog 工具是一个可以和shell脚本配合使用的文本界面下的创建对话框的工具.每个对话框提供的输出有两种形式: 1. 将所有输出到stderr 输出,不显示到屏幕. 2 ...
- dialog - 从 shell 显示对话框
总览 (SYNOPSIS) dialog --clear dialog --create-rc file dialog --print-maxsize dialog common-options bo ...
- shell之dialog提示窗口
dialog 提示窗口 1.msgbox dialog --msgbox text 20 10 2.yesno dialog --title "Please answer&q ...
- 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器
本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...
随机推荐
- 怎么配置Java环境变量?
右键计算机 -> 属性 -> 高级系统设置 -> 环境变量, 在系统环境变量添加以下三条变量. 1. PATH, 配置JDK命令文件的位置. 输入“%JAVA_HOME%\bin ...
- BizTalk开发系列(二十九) 宏的使用
在BizTalk中可以使用宏集合动态的让BizTalk发送处理程序使用单独的值来替换宏.常用的使用宏的发送程序有:文件发送适配器和SMTP发送适 配器.在表达式中可以使用同时使用多个宏.例如:在文件发 ...
- I/O存取方式的形象比喻
I/O存取有三种方式:可编程I/O.中断驱动I/O.DMA,分别可理解如下: 下面以老师向班里同学收发作业来类比I/O存取,办公室表示内存,即,I操作表示:老师向学生收作业,然后存放到办公室里:O操作 ...
- 使用SharpSSH连接服务器报Algorithm negotiation fail解决办法
SharpSSH或JSCH使用diffie-hellman-group1-sha1和diffie-hellman-group-exchange-sha1密钥交换算法,而OpenSSH在6.7p1版本之 ...
- what's the difference between dim as and dim as new?
what's the difference between dim as and dim as new? There is no difference with value types (Intege ...
- A trip through the Graphics Pipeline 2011_09_Pixel processing – “join phase”
Welcome back! This post deals with the second half of pixel processing, the “join phase”. The pre ...
- php 生成唯一id的几种解决方法
php 生成唯一id的几种解决方法 网上查了下,有很多的方法 1.md5(time() . mt_rand(1,1000000)); 这种方法有一定的概率会出现重复 2.php内置函数uniqid ...
- go gomail
package main //cmd: go get gopkg.in/gomail.v1 import ( "gopkg.in/gomail.v1" ) func main() ...
- apk反编译、smali修改、回编译笔记
最近下了一个apk程序,但是一启动会弹出一个流氓广告.这个广告不是原厂商加的,而是有人在原有apk程序的基础上,加了一个壳,让apk先启动他加的广告,再启动原来的程序,很恶心.于是想去掉它. 试了几个 ...
- 关于Action快捷键和小键盘的问题
在使用全尺寸键盘的时候 键盘右边都有一排小键盘 但是这个小键盘的数字键值和普通键盘的数字键值是不一样的 在ANSI码里 标准数字键值是$30..$39, 而小键盘的键值是$60..$69 这样问题就来 ...