Linux命令——whiptail交互式shell脚本对话框
转自:交互式shell脚本对话框----whiptail指令
当你在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/bashif (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/bashif (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/bashNAME=$(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:" $NAMEelse 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/bashPASSWORD=$(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:" $PASSWORDelse 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/bashOPTION=$(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:" $OPTIONelse 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/bashDISTROS=$(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:" $DISTROSelse 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/bashDISTROS=$(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:" $DISTROSelse 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 |
Linux命令——whiptail交互式shell脚本对话框的更多相关文章
- 【读书笔记】Linux命令行与Shell脚本编程大全
Linux命令行与Shell脚本编程大全 5.2 shell 的父子关系 命令分组 Command Grouping 主要有两种形式: 一种以小括号包括,命令之间以冒号分隔.也被称为 进程列表: 注意 ...
- 自学Linux命令行与Shell脚本之路
自学Linux命令行与Shell脚本之路[第一回]:初识Linux 1.1 自学Linux Shell1.1-Linux初识 1.2 自学Linux Shell1.2-Linux目录结构 1.3 ...
- Linux命令行与shell脚本编程大全.第3版(文字版) 超清文字-非扫描版 [免积分、免登录]
此处免费下载,无需账号,无需登录,无需积分.收集自互联网,侵权通知删除. 点击下载:Linux命令行与shell脚本编程大全.第3版 (大小:约22M)
- 《Linux命令行与shell脚本编程大全 第3版》创建实用的脚本---11
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---47
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---57
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---57
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---56
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---55
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
随机推荐
- LeetCode_463. Island Perimeter
463. Island Perimeter Easy You are given a map in form of a two-dimensional integer grid where 1 rep ...
- CSS继承控制:inherit、initial和unset
CSS里有三种常用的属性值继承方式:inherit,initial和unset.我们用一个简单的例子来演示一下: <ul style="color: green;"> ...
- nginx使用与配置入门指南
这是一篇关于nginx使用与配置的入门指南,但不包括nginx的编译与安装.我假定你知晓如何安装nginx.对大多数Linux系统来说,nginx都已经存在于它们的软件包里,直接使用系统提供的软件管理 ...
- js 强制换行及 单行文字溢出时出现省略号
/*强制换行*/.f-break {word-break:break-all; /*支持IE,chrome,FF不支持*/ word-wrap:break-word;/*支持IE,chrome,FF* ...
- scala 抽象类
package com.jason.qianfeng abstract class Person(val gender: String) { val name: String val age: Int ...
- Mysql 添加用户并授所有权
创建用户并授权GRANT ALL PRIVILEGES ON *.* TO 'caoxiaobo'@'%' IDENTIFIED BY 'Caoxiaobo0917!' WITH GRANT OPTI ...
- C++四大特性之封装
C++四大特性 C++作为面向对象编程语言,具备面向对象编程(Object Oriented Programming,OOP,面向对象程序设计)的四大特性.抽象,封装,继承,多态. 所谓抽象,就是对具 ...
- 长乐国庆集训Day5
T1 方阵 题目 [题目描述] 小澳最近迷上了考古,他发现秦始皇的兵马俑布局十分有特点,热爱钻研的小澳打算在电脑上还原这个伟大的布局. 他努力钻研,发现秦始皇布置兵马俑是有一定规律的.兵马俑阵总共有n ...
- 2019-7-17 正则表达式和re模块
一.re模块与正则表达式之间的关系 正则表达式不是python独有的,它是一门独立的技术 所有的编程语言都可以使用正则 但是如果你想在python中使用,你就必须依赖于re模块 正则的官方定义:正则表 ...
- Python-MySQL学习
内容来源(有删改):https://blog.csdn.net/hzw6991/article/details/87893761 上面链接同步视频地址:https://www.bilibili.com ...