Linux Shell窗口化

前段时间在学习Linux shell编程时,发现了几个比较好玩的工具,这里给大家分享一下…(Just for fun!)

以下三个关于创建窗口部件的包,一般应用在没有窗口界面的脚本(Script)中

1.Dialog

安装方法: apt-get install dialog

help:

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
cdialog (ComeOn Dialog!) version 1.3-20171209
Copyright 2000-2016,2017 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]
[--reorder] [--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.

dialog 命令选项(option)

–add-widget 继续下个对话框,直到按下Esc或Cancel按钮
–aspect ratio 指定窗口宽度和高度的宽高比
–backtitle title 指定显示在屏幕顶部背景上的标题
–begin x y 指定窗口左上角的起始位置
–cancel-label label 指定Cancel按钮的替代标签
–clear 用默认的对话背景色来清空屏幕内容
–colors 在对话文本中嵌入ANSI色彩编码
–cr-wrap 在对话文本中允许使用换行符并强制换行
–create-rc file 将示例配置文件的内容复制到指定的file文件中 1
–defaultno 将yes/no对话框的默认答案设为No
–default-item string 设定复选列表、表单或菜单对话中的默认项
–exit-label label 指定Exit按钮的替代标签
–extra-button 在OK按钮和Cancel按钮之间显示一个额外按钮
–extra-label label 指定额外按钮的替代标签
–help 显示dialog命令的帮助信息
–help-button 在OK按钮和Cancel按钮后显示一个Help按钮
–help-label label 指定Help按钮的替代标签
–help-status 当选定Help按钮后,在帮助信息后写入多选列表、单选列表或表单信息
–ignore 忽略dialog不能识别的选项
–input-fd fd 指定 STDIN 之外的另一个文件描述符
–insecure 在password部件中键入内容时显示星号
–item-help 为多选列表、单选列表或菜单中的每个标号在屏幕的底部添加一个帮助栏
–keep-window 不要清除屏幕上显示过的部件
–max-input size 指定输入的最大字符串长度。默认为2048
–nocancel 隐藏Cancel按钮
–no-collapse 不要将对话文本中的制表符转换成空格
–no-kill 将tailboxbg对话放到后台,并禁止该进程的 SIGHUP 信号
–no-label label 为No按钮指定替代标签
–no-shadow 不要显示对话窗口的阴影效果
–ok-label label 指定OK按钮的替代标签

注意: dialog 默认是重定向到标准错误(stderr).
不过可以改变输出到标准输出 –stdout .这样用脚本也就能很快获取返回的文本了

例子

$ dialog –timeout 5 –stdout –menu ‘Choose a job: ‘ 30 10 25 ‘Programmer’ 1 ‘Teacher’ 2 ‘Worker’ 3

example.sh

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
name=$(dialog --max-input 5 --timeout 5 --inputbox 'Please enter your name: ' 10 10 'root' --stdout)
job=$(dialog --timeout 5 --stdout --menu 'Choose a job: ' 30 10 25 'Programmer' 1 'Teacher' 2 'Worker' 3)
clear
if [ -n "$name" ];then
echo "Your name is: [$name]"
else
#do something...
fi
echo -e "You are a :"$job"\t:)"

2.whiptail

对比dialog,这个工具参数就少了一点,功能也就少了点,但基本的还是
要有的

There are visual differences which are not addressed by command-line options:

dialog centers lists within the window. whiptail typically puts lists against
the left margin.

whiptail uses angle brackets (“<” and “>”) for marking buttons. dialog uses
square brackets.

whiptail marks the limits of subtitles with vertical bars. dialog does not
mark the limits.

whiptail attempts to mark the top/bottom cells of a scrollbar with up/down ar‐
rows. When it cannot do this, it fills those cells with the background color
of the scrollbar and confusing the user. dialog uses the entire scrollbar
space, thereby getting better resolution.

注意

Most whiptail scripts write tostandard error, but error messages may also be written there, depend‐ing on your script.

help

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
Box options: 
--msgbox <text> <height> <width>
--yesno <text> <height> <width>
--infobox <text> <height> <width>
--inputbox <text> <height> <width> [init]
--passwordbox <text> <height> <width> [init]
--textbox <file> <height> <width>
--menu <text> <height> <width> <listheight> [tag item] ...
--checklist <text> <height> <width> <listheight> [tag item status]...
--radiolist <text> <height> <width> <listheight> [tag item status]...
--gauge <text> <height> <width> <percent>
Options: (depend on box-option)
--clear clear screen on exit
--defaultno default no button
--default-item <string> set default string
--fb, --fullbuttons use full buttons
--nocancel no cancel button
--yes-button <text> set text of yes button
--no-button <text> set text of no button
--ok-button <text> set text of ok button
--cancel-button <text> set text of cancel button
--noitem don't display items
--notags don't display tags
--separate-output output one line at a time
--output-fd <fd> output to fd, not stdout
--title <title> display title
--backtitle <backtitle> display backtitle
--scrolltext force vertical scrollbars
--topleft put window in top-left corner
-h, --help print this message
-v, --version print version information

例子

$ whiptail –title ‘Message’ –msgbox ‘Hi man~’ 10 20 –ok-button ‘OK’
$ whiptail –title ‘Login’ –output-fd 1 –inputbox ‘Please enter username: ‘ 10 20 ‘root’

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
#!/bin/bash
echo "" > .tmp_username
echo "" > .tmp_passphras
username=$(whiptail --title 'Login' --inputbox 'Please enter username: ' 10 20 'root' 1>&2 2>.tmp_username)
if [ "$?" -eq 0 ];then
username=$(cat .tmp_username)
if [[ -n $username ]];then
echo "==>"$username
else
echo 'Null Username!'
fi
else
echo "ERROR=> "$?
exit
fi
passphras=$(whiptail --title 'Login' --passwordbox 'Please enter password: ' 10 20 1>&2 2>.tmp_passphras)
if [ "$?" -eq 0 ];then
passphras=$(cat .tmp_passphras)
if [[ -n $passphras ]];then
echo "==>"$passphras
else
echo 'Null Passphras!'
fi
else
echo "ERROR=> "$?
exit
fi
rm .tmp_username
rm .tmp_passphras

3.Zenity

适用于 GNOME环境 的图形化界面包

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
zenity --help
用法:
zenity [OPTION…]
帮助选项:
-h, --help 显示帮助选项
--help-all 显示全部帮助选项
--help-general 显示常规选项
--help-calendar 显示日历选项
--help-entry 显示文字输入选项
--help-error 显示错误选项
--help-info 显示信息选项
--help-file-selection 显示文件选择选项
--help-list 显示列表选项
--help-notification 显示通知图标选项
--help-progress 显示进度选项
--help-question 显示问题选项
--help-warning 显示警告选项
--help-scale 显示范围选项
--help-text-info 显示文本信息选项
--help-color-selection 显示颜色选择选项
--help-password 显示口令对话框选项
--help-forms 显示表单对话框选项
--help-misc 显示杂类选项
--help-gtk 显示 GTK+ 选项
应用程序选项:
--calendar 显示日历对话框
--entry 显示文本输入对话框
--error 显示错误对话框
--info 显示信息对话框
--file-selection 显示文件选择对话框
--list 显示列表对话框
--notification 显示通知
--progress 显示进度指示对话框
--question 显示问题对话框
--warning 显示警告对话框
--scale 显示范围对话框
--text-info 显示文本信息对话框
--color-selection 显示颜色选择对话框
--password 显示口令对话框
--forms 显示表单对话框
--display=DISPLAY X display to use

$ zenity –help-info
用法:
zenity [OPTION…]

信息选项
–info 显示信息对话框
–text=文字 设置对话框文字
–icon-name=图标名 设置对话框图标
–no-wrap 不启用文本自动换行
–no-markup 不启用 pango 标记
–ellipsize 在对话框文本中启用简略显示。这可以修复带有长文本的窗口的尺寸过大。

更多信息 man

$ zenity –info –text=’Hello World’ –title=’Messge’ –width=30 –height=30 –timeout=3
$ find . -name ‘’ | zenity –progress –pulsate
$ find . -name ‘
’ | zenity –list –title “Search Results” –text “Finding all header files…” –column “Files”

结尾

现在,给出一个例子来实现MySQL数据库插入数据,用到了 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
exit_code=0
is_reg_ok=0
trap "echo;echo Have fun!;" EXIT
# get datetime
get_datenow(){
local now=$(date +'%Y-%m-%d %H:%M:%S')
echo $now
}
# start MYSQL server
start_MYSQL_server(){
echo 'Starting MYSQL Server...'
service mysql start
echo 'MHSQL Started!'
}
# showed user information when registered successfully
show_user_info(){
if [[ $is_reg_ok -eq 1 ]];then
echo
#shift
echo -e "UserName: [ \033[1;33m$1\033[0m ]"
echo -e "PassWord: [ \033[1;33m$2\033[0m ]"
echo -e "ID Name: [ \033[1;33m$3\033[0m ]"
echo -e "Register Date: [ \033[1;33m$4\033[0m ]"
echo
fi
}
############################################
#
# this is a bash script that
# quickly inset user data to mysql server database
#
############################################
echo
echo -e "Welcome to here ,you can register the account quickly...so let 's go!'"
echo
read -p 'Your username: ' -t 8 -r reg_user
if [ -z $reg_user ];then echo;echo 'Empty!'; exit;fi
read -p 'Your password: ' -t 8 -r reg_pass
if [ -z $reg_pass ];then echo;echo 'Empty!'; exit;fi
read -p 'Your password[confirm]: ' -s -t 8 -r reg_pass_confirm
if [ -z $reg_pass_confirm ];then echo;echo 'Password not same!'; exit;fi
echo
read -p 'Your ID name: ' -t 8 -r reg_idname
if [ -z $reg_idname ];then echo;echo 'Empty!'; exit;fi
start_MYSQL_server
# here append a --stdout instead of default --stderr,so we can get the result....
username=$(dialog --max-input 8 --timeout 8 --inputbox 'Please enter MYSQL username: ' 10 10 'root' --stdout)
exit_code=$?
password=$(dialog --max-input 15 --timeout 8 --passwordbox 'Please enter MYSQL password: ' 10 10 --stdout)
clear
# check the username is empty
if [ "$exit_code" -eq 0 ];then
if [ -z "$username" ] ;then
echo -e "\033[1;31mNot Empty Username!\033[0m"
exit
fi
else
exit
fi
mysql_string="mysql -u${username} -p${password}"
now_string=$(get_datenow)
# register ==> insert data to mysql
${mysql_string} --execute="INSERT INTO db_info.tb_users (username,password,idname,reg_datetime) values ('${reg_user}','${reg_pass_confirm}','${reg_idname}','${now_string}');"
if [ "$?" -eq 0 ];then
echo -e "\033[1;32mRegister successfully!\033[0m"
let is_reg_ok=1
show_user_info $reg_user $reg_pass_confirm $reg_idname $now_string
exit 0
else
echo -e "\033[1;31mRegister failed!\033[0m"
exit 1
fi

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!