dialog linux 详解
时间:2014-08-06 11:53 来源:linux.it.net.cn 作者:it
本文讲解linux shell下的dialog命令,并给出一个使用的例子,如果你需要使用类似场景,可以直接对示例进行修改即可。

使用linux的人,一天到晚都是面对着shell。但如果是开发一个程序,这个程序是给初学者使用的,那shell就不再友好。这时就可以使用dialog来生成简单的图形界面。
dialog安装
dialog只有一个文件,也算是绿色版的吧。
首先下载dialog程序,下载地址:dialog.tar.bz2
然后解压到相应位置,并修改权限即可:
1
2
3
tar jxvf dialog.tar.bz2
cp dialog /usr/bin
chmod 755 /usr/bin/dialog
dialog – linux shell script示例
将以下代码放在一个文件中,假设文件名为test,修改权限:
1
chmod 755 test
运行命令 ./test 即可运行了。
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
64
65
66
#!/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
脚本简单说明
脚本的作用是对某个程序设置一些简单的内容,如用户名、密码、职业等,包括了信息框、输入框、密码输入框、单选项等组件的使用。
在运行时会显示5个窗口,这5个窗口顺序执行,可以返回上一步。
dialog 选项
对于上述脚本中dialog的选项我不作具体说明,因为只要运行之后,对照脚本内容,很容易知道dialog每个参数的作用。
1
2>/tmp/xxx
这是表示将dialog命令中的输入的内容(保存在stderr)存放到/tmp/xxx文件。
dialog命令的返回值包括:
-
0 : 正确执行结果或选择肯定项:yes 或 ok
-
1 : 选择否定项:no 或者 cancel
-
255 :按esc键退出的返回值
dialog 所有选项:
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
[root@localhost ~]# dialog --help
cdialog (ComeOn Dialog!) version 1.1-20080819
Copyright 2000-2007,2008 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>]
[--begin <y> <x>] [--cancel-label <str>] [--clear] [--colors]
[--column-separator <str>] [--cr-wrap] [--default-item <str>]
[--defaultno] [--exit-label <str>] [--extra-button]
[--extra-label <str>] [--help-button] [--help-label <str>]
[--help-status] [--ignore] [--input-fd <fd>] [--insecure]
[--item-help] [--keep-tite] [--keep-window] [--max-input <n>]
[--no-cancel] [--no-collapse] [--no-kill] [--no-label <str>]
[--no-lines] [--no-ok] [--no-shadow] [--nook] [--ok-label <str>]
[--output-fd <fd>] [--output-separator <str>] [--print-maxsize]
[--print-size] [--print-version] [--quoted] [--separate-output]
[--separate-widget <str>] [--shadow] [--single-quoted] [--size-err]
[--sleep <secs>] [--stderr] [--stdout] [--tab-correct] [--tab-len <n>]
[--timeout <secs>] [--title <title>] [--trace <file>] [--trim]
[--version] [--visit-items] [--yes-label <str>]
Box options:
--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>
--progressbox <height> <width>
--radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...
--tailbox <file> <height> <width>
--tailboxbg <file> <height> <width>
--textbox <file> <height> <width>
--timebox <text> <height> <width> <hour> <minute> <second>
--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.
dialog安装常见问题
缺少glibc依赖组件
在运行dialog时提示:
1
-bash: /usr/bin/dialog: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录
或下面的提示:
1
/lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
解决方法:安装 glibc.i686包,在Redhat或CentOS中运行以下命令即可:
1
yum install glibc.i686
缺少libncurses依赖组件
在运行dialog时提示:
1
dialog: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
解决方法: 安装 x86_32 的ncurses包即可,在Redhat或CentOS中运行以下命令即可:
1
yum install ncurses
(责任编辑:IT)
本文讲解linux shell下的dialog命令,并给出一个使用的例子,如果你需要使用类似场景,可以直接对示例进行修改即可。
使用linux的人,一天到晚都是面对着shell。但如果是开发一个程序,这个程序是给初学者使用的,那shell就不再友好。这时就可以使用dialog来生成简单的图形界面。 dialog安装dialog只有一个文件,也算是绿色版的吧。 首先下载dialog程序,下载地址:dialog.tar.bz2 然后解压到相应位置,并修改权限即可:
dialog – linux shell script示例将以下代码放在一个文件中,假设文件名为test,修改权限:
运行命令 ./test 即可运行了。
脚本简单说明脚本的作用是对某个程序设置一些简单的内容,如用户名、密码、职业等,包括了信息框、输入框、密码输入框、单选项等组件的使用。 在运行时会显示5个窗口,这5个窗口顺序执行,可以返回上一步。 dialog 选项对于上述脚本中dialog的选项我不作具体说明,因为只要运行之后,对照脚本内容,很容易知道dialog每个参数的作用。
这是表示将dialog命令中的输入的内容(保存在stderr)存放到/tmp/xxx文件。 dialog命令的返回值包括:
dialog 所有选项:
dialog安装常见问题缺少glibc依赖组件在运行dialog时提示:
或下面的提示:
解决方法:安装 glibc.i686包,在Redhat或CentOS中运行以下命令即可:
缺少libncurses依赖组件在运行dialog时提示:
解决方法: 安装 x86_32 的ncurses包即可,在Redhat或CentOS中运行以下命令即可:
(责任编辑:IT) |