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

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

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

语法:

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 

创建一个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) then echo "You chose Yes. Exit status was $?." else echo "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) then echo "You chose Skittles Exit status was $?." else echo "You chose M&M's. Exit status was $?." fi 

创建一个表单输入框
如果你想用户输入任意的文本,您可以使用一个输入框。

语法:

 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 ]; then echo "Your pet name is:" $PET else echo "You chose Cancel." fi 

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

语法:

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 ]; then echo "Your password is:" $PASSWORD else echo "You chose Cancel." fi 

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

语法:

 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 ]; then echo "Your chosen option:" $OPTION else echo "You chose Cancel." fi 

创建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 ]; then echo "The chosen distro is:" $DISTROS else echo "You chose Cancel." fi 

创建一个表对话框
当你想让用户选择一个列表中选择多个选项的清单对话框是有用的,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 ]; then echo "Your favorite distros are:" $DISTROS else echo "You chose Cancel." fi 

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

语法:

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

实例:

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

本文转载自:http://www.linuxprobe.com/create-interactive-shell-script.html

更多Linux干货请访问:http://www.linuxprobe.com/

创建交互式shell脚本对话框的更多相关文章

  1. Linux命令——whiptail交互式shell脚本对话框

    转自:交互式shell脚本对话框----whiptail指令 当你在linux环境下setup软件的时候就会有相应的对话框让你输入.虽然我们已经习惯了这种交互的方法,但是如果有一种直观的界面来输入是不 ...

  2. 交互式shell脚本对话框----whiptail指令

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

  3. 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器

    本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...

  4. 交互式shell脚本web console

    官网:http://web-console.org/ 这个脚本可以实现web下交互,也就是有了这玩意后可以不用反弹shell了. <?php // Web Console v0.9.7 (201 ...

  5. 非交互式shell脚本案例-实现自主从oracle数据库获取相关数据,并在制定目录生成相应规则的文件脚本

    get_task_id 脚本内容 #!/usr/bin/expect#配置登陆数据库的端口set port 22#配置登陆数据库的ip地址set oracleip 10.0.4.41#配置数据库实例名 ...

  6. 创建和运行shell脚本程序

    转载请标明http://www.cnblogs.com/winifred-tang94/ 要创建一个shell脚本程序,首先新建一个文本文件,然后在这个文本文件中按照shell编程规则输入shell命 ...

  7. 创建shell脚本文件

    简单来说脚本就是将需要执行的命令保存到文本中,按照顺序(由上往下执行),它是解释型的,不需要 编译 脚本格式 #!/bin/bash或者#!/bin/env bash开头 第一个shell脚本:hel ...

  8. Shell脚本之九 输入输出重定向和文件包含

    输出重定向:是指不使用系统提供的标准输入端口来输出,而是重新指定其他来进行输出.例如在终端输入的字符串本来是要输出到终端屏幕上的,但可以将输出指定为其他文件,将输入字符串输出到该文件中,而不再是屏幕上 ...

  9. Shell脚本 (一) 概述、解析器、脚本入门

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一. Shell 脚本概述 1. Shell 的 含义: Shell 是一个用C语言编写的程序,它是用户 ...

随机推荐

  1. 解决:操作无法完成(错误0x00000709)。再次检查打印机名称,并确保打印机已连接到.

    就是重启一下服务器端的Print Spooler服务就行了,这么简单! [控制面板 -  服务 -  Print Spooler]

  2. C- struct的使用

    数组是二等公民,不能进行整体赋值,或者参数传递,或者作为返回值. But!如果封装在struct内部,就完全不一样了 #include <iostream> using namespace ...

  3. IT公司100题-3-求数组的最大子序列的和

    问题描述: 输入一个整形数组,数组里有正数也有负数. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 求所有子数组的和的最大值.要求时间复杂度为O(n). 例如输入的数组为1, -2 ...

  4. javax.servlet.ServletException: Error instantiating servlet class Compiler 错误

    javax.servlet.ServletException: Error instantiating servlet class Compiler org.apache.catalina.authe ...

  5. WP8.1 Study5:Data binding数据绑定

    一.数据绑定 最简单的编程UI控件的方法是写自己的数据来获取和设置控件的属性,e.g. , textBox1.Text = "Hello, world"; 但在复杂的应用程序,这样 ...

  6. java面试题之ssh

    1.写出你熟悉的开源框架以及各自的作用(项目中为什么使用SSH) 答:框架:hibernate,spring,struts1/struts2. Hibernate主要用于数据持久化:封装了JDBC操作 ...

  7. HTML--9表单和验证事件

    1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电话号码, ...

  8. Android类参考---Fragment

    Android类参考---Fragment public final boolean isAdded() 如果该Fragment对象被添加到了它的Activity中,那么它返回true,否则返回fal ...

  9. 小米Recovery线刷精灵 v1.0.0 破解版

    下载地址:http://www.crsky.com/soft/75923.html 小米Recovery线刷精灵支持将Recovery线刷包一键刷入小米手机,支持小米所有型号. 小米Recovery线 ...

  10. PIT,BL,AP,CP,CSC

    使用ODIN刷机的时候,要选择ROM文件,以下是5件套各部分的说明: PIT:分区信息,如果没有更换ROM,一般不需要刷,也不需要勾选re-partition选项 BL:bootloader,引导信息 ...