这个教程给出几个如何使用类似zenity和whiptail的工具在bash shell 脚本中提供消息/对话框的例子。使用这些工具,你的脚本能够告知用户当前程序运行的状态并能与用户进行交互。这两个工具的不同之处在于显示消息框或者对话框的方式。zenity用gtk工具包创建图形用户界面,而whiptail则在终端窗口内创建消息框。
<a target="_blank"></a>
在ubuntu中安装zenity,运行:
<code>sudo apt-get install zenity</code>
用zenity创建消息框或者对话框的命令是不言自明的,我们会给你提供一些例子来参考。
<code>zenity --info --title "information box" --text "this should be information" --width=300 --height=200</code>

消息框截图
创建 yes/no 询问对话框
<code>zenity --question --text "do you want this?" --ok-label "yeah" --cancel-label="nope"</code>
问题截图
创建输入框并将输入值保存到变量中
<code>a=$(zenity --entry --title "entry box" --text "please enter the value" --width=300 --height=200)</code>
<code>echo $a</code>
输入框截图
输入后,值会保存在变量 $a 中。
这是一个获取用户姓名并显示的实际事例。
<code>#!/bin/bash</code>
<code>#</code>
<code># this script will ask for couple of parameters</code>
<code># and then continue to work depending on entered values</code>
<code></code>
<code># giving the option to user</code>
<code>zenity --question --text "do you want to continue?"</code>
<code># checking if user wants to proceed</code>
<code>[ $? -eq 0 ] || exit 1</code>
<code># letting user input some values</code>
<code>firstname=$(zenity --entry --title "entry box" --text "please, enter your first name." --width=300 --height=150)</code>
<code>lastname=$(zenity --entry --title "entry box" --text "please, enter your last name." --width=300 --height=150)</code>
<code>age=$(zenity --entry --title "entry box" --text "please, enter your age." --width=300 --height=150)</code>
<code># displaying entered values in information box</code>
<code>zenity --info --title "information" --text "you are ${firstname} ${lastname} and you are ${age}(s) old." --width=300 --height=100</code>
这些是运行前面脚本的截图。
例1-问题-1
框1
例1-输入框-1
输入框
例1-输入框-2
例1-输入框-3
例1-信息
信息框
在ubuntu上安装whiptail,运行
<code>sudo apt-get install whiptail</code>
用whiptail创建消息框或者对话框的命令也是无需解释的,我们会给你提供一些基本例子作为参考。
<code>whiptail --msgbox "this is a message" 10 40</code>
whiptail消息框截图
<code>whiptail --yes-button "yeah" --no-button "nope" --title "choose the answer" --yesno "will you choose yes?" 10 30</code>
whiptail对话框截图
<code>whiptail --inputbox "enter your number please." 10 30 "10"</code>
whiptail输入框截图
尝试使用输入值要注意的一点是whiptail用stdout显示对话框,用stderr输出值。这样的话,如果你用 var=$(...),你就根本不会看到对话框,也不能获得输入的值。解决方法是交换stdout和stderr。在whiptail命令后面添加 3>&1 1>&2 2>&3 就可以做到。你想获取输入值的任何whiptail命令也是如此。
<code>whiptail --menu "this is a menu. choose an option:" 20 50 10 1 "first" 2 "second" 3 "third"</code>
whiptail菜单截图
这是一个请求用户输入一个文件夹的路径并输出它的大小的 shell 脚本。
<code># since whiptail has to use stdout to display dialog, entered value will</code>
<code># be stored in stderr. to switch them and get the value to stdout you must</code>
<code># use 3>&1 1>&2 2>&3</code>
<code>folder_path=$(whiptail --title "get the size of folder" \</code>
<code>--inputbox "enter folder path:" \</code>
<code>10 30 \</code>
<code>"/home" \</code>
<code>3>&1 1>&2 2>&3)</code>
<code>if [ -d $folder_path ]</code>
<code>then</code>
<code>size=$(du -hs "$folder_path" | awk '{print $1}')</code>
<code>whiptail --title "information" \</code>
<code>--msgbox "size of ${folder_path} is ${size}" \</code>
<code>10 40</code>
<code>elif [ -f $folder_path ]</code>
<code>whiptail --title "warning!!!" \</code>
<code>--msgbox "the path you entered is a path to a file not a folder!" \</code>
<code>else</code>
<code>whiptail --title "error!!!"</code>
<code>--msgbox "path you entered is not recognized. please try again" \</code>
<code>fi</code>
这是之前例子的一些截图:
例2-输入框
例2-消息框
消息框
选择合适的工具显示对话框取决于你期望在桌面机器还是服务器上运行你的脚本。桌面机器用户通常使用gui窗口环境,也可能运行脚本并与显示的窗口进行交互。然而,如果你期望用户是在服务器上工作的,(在没有图形界面时,)你也许希望能确保总能显示,那就使用whiptail或者任何其它在纯终端窗口显示对话框的工具。
原文发布时间为:2015-06-02
本文来自云栖社区合作伙伴“linux中国”