交互式shell脚本编程2

news/2024/10/18 5:49:35/

当你在终端环境下安装新的软件时,你可以经常看到信息对话框弹出,需要你的输入,比如:RHEL/CentOS自带的setup,对话框的类型有密码箱、检查表、菜单等等。他们可以引导你以一种直观的方式输入必要的信息,使用这样的用户友好的对话框的好处是显而易见的。如下图所示:
在这里插入图片描述

当你写一个交互式shell脚本,你可以使用这样的对话框来接受用户的输入。whiptail可以在shell脚本中创建基于终端的对话框,消息框的过程,类似于Zenity或xdialog GUI脚本代码。whiptail预先安装在所有的Linux发布版本中。

1. 创建一个消息框中显示一个确认按钮继续任意的文本消息。

语法:

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

实例:

#!/bin/bash
whiptail --title "Test Message Box" --msgbox "Create a message box with whiptail. Choose Ok to continue." 10 60

在这里插入图片描述

2. 创建一个yes/no对话框用户输入yes或no的对话框。

语法:

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

实例:

#!/bin/bash
if (whiptail --title "Test Yes/No Box" --yesno "Choose between Yes and No." 10 60) thenecho "You chose Yes. Exit status was $?."
elseecho "You chose No. Exit status was $?."
fi

或者,你可以是“–yes-button” ,"–no-button"选项。

#!/bin/bash
if (whiptail --title "Test Yes/No Box" --yes-button "Skittles" --no-button "M&M's"  --yesno "Which do you like better?" 10 60) thenecho "You chose Skittles Exit status was $?."
elseecho "You chose M&M's. Exit status was $?."
fi

3. 创建一个表单输入框如果你想用户输入任意的文本

语法:

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

实例:

#!/bin/bash
PET=$(whiptail --title "Test Free-form Input Box" --inputbox "What is your pet's name?" 10 60 Wigglebutt 3>&1 1>&2 2>&3)exitstatus=$?
if [ $exitstatus = 0 ]; thenecho "Your pet name is:" $PET
elseecho "You chose Cancel."
fi

4. 创建一个密码框当用户需要输入敏感信息时密码框是有用的。

语法:

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

实例:

#!/bin/bash
PASSWORD=$(whiptail --title "Test Password Box" --passwordbox "Enter your password and choose Ok to continue." 10 60 3>&1 1>&2 2>&3)exitstatus=$?
if [ $exitstatus = 0 ]; thenecho "Your password is:" $PASSWORD
elseecho "You chose Cancel."
fi

5. 创建一个菜单栏当你想让用户选择一个任意数量的选择中,你可以使用菜单框。

语法:

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

实例:

#!/bin/bash
OPTION=$(whiptail --title "Test Menu Dialog" --menu "Choose your option" 15 60 4 \
"1" "Grilled Spicy Sausage" \
"2" "Grilled Halloumi Cheese" \
"3" "Charcoaled Chicken Wings" \
"4" "Fried Aubergine"  3>&1 1>&2 2>&3)exitstatus=$?
if [ $exitstatus = 0 ]; thenecho "Your chosen option:" $OPTION
elseecho "You chose Cancel."
fi

6. 创建radiolist对话框

语法:

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

实例:

#!/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 ]; thenecho "The chosen distro is:" $DISTROS
elseecho "You chose Cancel."
fi

7. 创建一个表对话框当你想让用户选择一个列表中选择多个选项的清单对话框是有用的,radiolist对话框,只允许选择一个。

语法:

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

实例:

#!/bin/bash
DISTROS=$(whiptail --title "Test 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 ]; thenecho "Your favorite distros are:" $DISTROS
elseecho "You chose Cancel."
fi

8. 创建一个进度条进度条是一个用户友好的对话框。whiptail从标准输入读取一个百分数(0~100),显示一个表内相应的计数。

语法:

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

实例:

#!/bin/bash
{for ((i = 0 ; i <= 100 ; i+=20)); dosleep 1echo $idone
} | whiptail --gauge "Please wait while installing" 6 60 0

http://www.ppmy.cn/news/47219.html

相关文章

精通 TensorFlow 1.x:16~19

原文&#xff1a;Mastering TensorFlow 1.x 协议&#xff1a;CC BY-NC-SA 4.0 译者&#xff1a;飞龙 本文来自【ApacheCN 深度学习 译文集】&#xff0c;采用译后编辑&#xff08;MTPE&#xff09;流程来尽可能提升效率。 不要担心自己的形象&#xff0c;只关心如何实现目标。—…

UDS统一诊断服务【五】诊断仪在线0X3E服务

文章目录 前言一、诊断仪在线服务介绍二、数据格式2.1&#xff0c;请求报文2.2&#xff0c;子功能2.3&#xff0c;响应报文 前言 本文介绍UDS统一诊断服务的0X3E服务&#xff0c;希望能对你有所帮助 一、诊断仪在线服务介绍 诊断仪在线服务比较简单&#xff0c;其功能就是告诉…

【JavaEE】File、InputStream和OutputStream的使用

1.File 在计算机中目录结构如下&#xff1a; 而File就表示一个目录或者一个普通文件。 File表示目录&#xff1a; File表示普通文件&#xff1a; 我们先来看File的构造方法&#xff1a; 构造器描述File(File parent, String child)根据父目录 孩子文件路径&#xff0c;创…

玄子Share - 精选三套 JavaScript 练手项目

玄子Share - 精选三套 JavaScript 练手项目 1. 50 Projects in 50 Days - HTML/CSS and JavaScript 50 天 50 个前端练手项目 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dCWO6V2G-1682076972984)(./assets/image-20230421192413713.png)] [外链…

VUE3的使用

文章目录 一、Vue3基础语法1、Vue开发准备2、Vue的模板语法3、条件渲染4、列表渲染5、事件处理6、表单输入绑定 二、Components组件1、组件基础2、组件交互3、自定义事件的组件交互4、组件生命周期5、引入第三方组件 三、网络请求及路由配置1、Axios网络请求2、Axios网络请求封…

【rustdesk】rust入门及 windows尝试编译

rustup 微软建议用vs code开发 下载了64位的版本: vs code 插件 rust-analyer 介绍Better TOML,用于更好的展示.toml文件Error Lens, 更好的获得错误展示 One Dark Pro, 非常好看的Vscode主题 CodeLLDB, debugger程序 安装

vue:生成二维码 qrcode、vue-qr(二维码中间可带logo)

一、方法一 qrcode qrcode - npm 1.1、安装 yarn add qrcode 1.2、页面引入 import QRCode from qrcode; 1.3、方法里边使用 getQRCodeUrl(){ QRCode.toDataURL(hello world,{color: {dark:"#010599FF",light:"#FFBF60FF"}}).then((url) > {// 获…

循环结构化命令小结

shell脚本编程系列 循环是编程不可或缺的一部分。bash shell提供了三种循环命令。 for命令允许遍历一系列的值&#xff0c;无论是在命令行中提供的&#xff0c;还是包含在变量中的&#xff0c;或是通过文件名通配符匹配获得的文件名和目录名。 for var in list docommands do…