博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux命令——whiptail交互式shell脚本对话框
阅读量:7211 次
发布时间:2019-06-29

本文共 3726 字,大约阅读时间需要 12 分钟。

转自:

当你在linux环境下setup软件的时候就会有相应的对话框让你输入。虽然我们已经习惯了这种交互的方法,但是如果有一种直观的界面来输入是不是会更加友好和方便呢,在shell脚本中你可以使用-whiptail指令来完成。

消息框

语法:

1
whiptail 
-
-
title 
"<message box title>" 
-
-
msgbox 
"<text to show>" 
<height> <width>

实例:

1
whiptail 
-
-
title 
"Message box title" 
-
-
msgbox 
" Choose Ok to continue." 
10 
60

 yes/no对话框

语法:

1
whiptail 
-
-
title 
"<dialog box title>" 
-
-
yesno 
"<text to show>" 
<height> <width>

实例:

1
2
3
4
5
6
#!/bin/bash
if 
(whiptail 
-
-
title 
"Yes/No Box" 
-
-
yesno 
"Choose between Yes and No." 
10 
60
) then
    
echo 
"You chose Yes. Exit status was $?."
else
    
echo 
"You chose No. Exit status was $?."
fi

或者也可以是自定义的选项,实例如下:

1
2
3
4
5
6
#!/bin/bash
if 
(whiptail 
-
-
title 
"Yes/No Box" 
-
-
yes
-
button 
"Man" 
-
-
no
-
button 
"Woman"  
-
-
yesno 
"What is your gender?" 
10 
60
) then
    
echo 
"You chose Man Exit status was $?."
else
    
echo 
"You chose Woman. Exit status was $?."
fi

当选择左边选项的时候输出的是0,选择右边选项的时候输出的是1。

表单输入框

语法:

1
whiptail 
-
-
title 
"<input box title>" 
-
-
inputbox 
"<text to show>" 
<height> <width> <default
-
text>

实例:

1
2
3
4
5
6
7
8
9
#!/bin/bash
NAME
=
$(whiptail 
-
-
title 
"Free-form Input Box" 
-
-
inputbox 
"What is your pet's name?" 
10 
60 
Peter 
3
>&
1 
1
>&
2 
2
>&
3
)
 
exitstatus
=
$?
if 
[ $exitstatus 
= 
0 
]; then
    
echo 
"Your name is:" 
$NAME
else
    
echo 
"You chose Cancel."
fi

 密码输入框

 

语法:

1
whiptail 
-
-
title 
"<password box title>" 
-
-
passwordbox 
"<text to show>" 
<height> <width>

实例:

1
2
3
4
5
6
7
8
9
#!/bin/bash
PASSWORD
=
$(whiptail 
-
-
title 
"Password Box" 
-
-
passwordbox 
"Enter your password and choose Ok to continue." 
10 
60 
3
>&
1 
1
>&
2 
2
>&
3
)
 
exitstatus
=
$?
if 
[ $exitstatus 
= 
0 
]; then
    
echo 
"Your password is:" 
$PASSWORD
else
    
echo 
"You chose Cancel."
fi

菜单栏

提供一个单项选择的菜单栏

语法:

1
whiptail 
-
-
title 
"<menu title>" 
-
-
menu 
"<text to show>" 
<height> <width> <menu height> [ <tag> <item> ] . . .

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash
OPTION
=
$(whiptail 
-
-
title 
"Menu Dialog" 
-
-
menu 
"Choose your favorite programming language." 
15 
60 
4 
\
"1" 
"Python" 
\
"2" 
"Java" 
\
"3" 
"C" 
\
"4" 
"PHP"  
3
>&
1 
1
>&
2 
2
>&
3
)
 
exitstatus
=
$?
if 
[ $exitstatus 
= 
0 
]; then
    
echo 
"Your favorite programming language is:" 
$OPTION
else
    
echo 
"You chose Cancel."
fi

此处的选择输出的内容为你选择的标签的‘tag’位置,上面实例中的‘1’、‘2’、‘3’、‘4’

 radiolist对话框

该对话框是单选对话框,你可以控制默认的选择位置,即使你在脚本中默认选择多个,他也只会输出一个结果

 

语法:

1
whiptail 
-
-
title 
"<radiolist title>" 
-
-
radiolist 
"<text to show>" 
<height> <width> <
list 
height> [ <tag> <item> <status> ] . . .

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
DISTROS
=
$(whiptail 
-
-
title 
"Test Checklist Dialog" 
-
-
radiolist \
"What is the Linux distro of your choice?" 
15 
60 
4 
\
"debian" 
"Venerable Debian" 
ON \
"ubuntu" 
"Popular Ubuntu" 
OFF \
"centos" 
"Stable CentOS" 
OFF \
"mint" 
"Rising Star Mint" 
OFF 
3
>&
1 
1
>&
2 
2
>&
3
)
 
exitstatus
=
$?
if 
[ $exitstatus 
= 
0 
]; then
    
echo 
"The chosen distro is:" 
$DISTROS
else
    
echo 
"You chose Cancel."
fi  

多选对话框

 

语法:

1
whiptail 
-
-
title 
"<checklist title>" 
-
-
checklist 
"<text to show>" 
<height> <width> <
list 
height> [ <tag> <item> <status> ] . . .

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
DISTROS
=
$(whiptail 
-
-
title 
"Checklist Dialog" 
-
-
checklist \
"Choose preferred Linux distros" 
15 
60 
4 
\
"debian" 
"Venerable Debian" 
ON \
"ubuntu" 
"Popular Ubuntu" 
OFF \
"centos" 
"Stable CentOS" 
ON \
"mint" 
"Rising Star Mint" 
OFF 
3
>&
1 
1
>&
2 
2
>&
3
)
 
exitstatus
=
$?
if 
[ $exitstatus 
= 
0 
]; then
    
echo 
"Your favorite distros are:" 
$DISTROS
else
    
echo 
"You chose Cancel."
fi  

进度条

语法:

1
whiptail 
-
-
gauge 
"<test to show>" 
<height> <width> <inital percent>

实例:

1
2
3
4
5
6
7
#!/bin/bash
{
    
for 
((i 
= 
0 
; i <
= 
100 
; i
+
=
20
)); do
        
sleep 
1
        
echo $i
    
done
} | whiptail 
-
-
gauge 
"Please wait while installing" 
6 
60 
0

转载于:https://www.cnblogs.com/kelamoyujuzhen/p/10848043.html

你可能感兴趣的文章
各数据库连接配置与maven依赖安装
查看>>
Linux(centOS)手动安装删除Apache+MySQL+PHP+Memcached原创无错版
查看>>
Nginx的启动(start),停止(stop)命令
查看>>
代码生成工具更新--快速生成Winform框架的界面项目
查看>>
Jquery根据JSON生成Table
查看>>
[Oracle]Sqlplus 中使用 new_value
查看>>
【HTTP】 认证和单点登录 【瞎写的…】
查看>>
微信小程序-上传多张图片加进度条(支持预览、删除)
查看>>
Java基础-SSM之mybatis快速入门篇
查看>>
error C2220: 警告被视为错误 - 没有生成“object”文件
查看>>
IO is frozen on database xxx, No user action is required
查看>>
执行perl xttdriver.pl报错Can't locate Getopt/Long.pm in @INC
查看>>
log4j的最佳实践(转)
查看>>
linux中 jdk 的卸载和安装[转]
查看>>
Install And Configure ColdFusion MX 6.1 on Windows
查看>>
[转]Error: "SQL BPA command line has encountered a problem and needs to close"
查看>>
objective-C 的内存管理之-引用计数
查看>>
如何实现共享软件网络授权认证,包括注册新用户、登录、修改密码等操作
查看>>
通过TextWatcher去观察输入框中输入的内容以及输入字符个数
查看>>
C#窗体控件-单选按钮控件RadioButton
查看>>