如何在Bash Shell脚本中显示对话框
这个教程给出几个如何使用类似zenity和whiptail的工具在Bash Shell 脚本中提供消息/对话框的例子。使用这些工具,你的脚本能够告知用户当前程序运行的状态并能与用户进行交互。这两个工具的不同之处在于显示消息框或者对话框的方式。Zenity用GTK工具包创建图形用户界面,而whiptail则在终端窗口内创建消息框。
Zenity 工具
在Ubuntu中安装zenity,运行:
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install zenity</span>
用zenity创建消息框或者对话框的命令是不言自明的,我们会给你提供一些例子来参考。
创建消息框
<span class="pln">zenity </span><span class="pun">--</span><span class="pln">info </span><span class="pun">--</span><span class="pln">title </span><span class="str">"Information Box"</span><span class="pun">--</span><span class="pln">text </span><span class="str">"This should be information"</span><span class="pun">--</span><span class="pln">width</span><span class="pun">=</span><span class="lit">300</span><span class="pun">--</span><span class="pln">height</span><span class="pun">=</span><span class="lit">200</span>
创建 Yes/No 询问对话框
<span class="pln">zenity </span><span class="pun">--</span><span class="pln">question </span><span class="pun">--</span><span class="pln">text </span><span class="str">"Do you want this?"</span><span class="pun">--</span><span class="pln">ok</span><span class="pun">-</span><span class="pln">label </span><span class="str">"Yeah"</span><span class="pun">--</span><span class="pln">cancel</span><span class="pun">-</span><span class="pln">label</span><span class="pun">=</span><span class="str">"Nope"</span>
创建输入框并将输入值保存到变量中
<span class="pln">a</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="pln">zenity </span><span class="pun">--</span><span class="pln">entry </span><span class="pun">--</span><span class="pln">title </span><span class="str">"Entry box"</span><span class="pun">--</span><span class="pln">text </span><span class="str">"Please enter the value"</span><span class="pun">--</span><span class="pln">width</span><span class="pun">=</span><span class="lit">300</span><span class="pun">--</span><span class="pln">height</span><span class="pun">=</span><span class="lit">200</span><span class="pun">)</span>
<span class="pln">echo $a</span>
输入后,值会保存在变量 $a 中。
这是一个获取用户姓名并显示的实际事例。
<span class="com">#!/bin/bash</span>
<span class="com">#</span>
<span class="com"># This script will ask for couple of parameters</span>
<span class="com"># and then continue to work depending on entered values</span>
<span class="com">#</span>
<span class="com"># Giving the option to user</span>
<span class="pln">zenity </span><span class="pun">--</span><span class="pln">question </span><span class="pun">--</span><span class="pln">text </span><span class="str">"Do you want to continue?"</span>
<span class="com"># Checking if user wants to proceed</span>
<span class="pun">[</span><span class="pln"> $</span><span class="pun">?</span><span class="pun">-</span><span class="pln">eq </span><span class="lit">0</span><span class="pun">]</span><span class="pun">||</span><span class="kwd">exit</span><span class="lit">1</span>
<span class="com"># Letting user input some values</span>
<span class="pln">FIRSTNAME</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="pln">zenity </span><span class="pun">--</span><span class="pln">entry </span><span class="pun">--</span><span class="pln">title </span><span class="str">"Entry box"</span><span class="pun">--</span><span class="pln">text </span><span class="str">"Please, enter your first name."</span><span class="pun">--</span><span class="pln">width</span><span class="pun">=</span><span class="lit">300</span><span class="pun">--</span><span class="pln">height</span><span class="pun">=</span><span class="lit">150</span><span class="pun">)</span>
<span class="pln">LASTNAME</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="pln">zenity </span><span class="pun">--</span><span class="pln">entry </span><span class="pun">--</span><span class="pln">title </span><span class="str">"Entry box"</span><span class="pun">--</span><span class="pln">text </span><span class="str">"Please, enter your last name."</span><span class="pun">--</span><span class="pln">width</span><span class="pun">=</span><span class="lit">300</span><span class="pun">--</span><span class="pln">height</span><span class="pun">=</span><span class="lit">150</span><span class="pun">)</span>
<span class="pln">AGE</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="pln">zenity </span><span class="pun">--</span><span class="pln">entry </span><span class="pun">--</span><span class="pln">title </span><span class="str">"Entry box"</span><span class="pun">--</span><span class="pln">text </span><span class="str">"Please, enter your age."</span><span class="pun">--</span><span class="pln">width</span><span class="pun">=</span><span class="lit">300</span><span class="pun">--</span><span class="pln">height</span><span class="pun">=</span><span class="lit">150</span><span class="pun">)</span>
<span class="com"># Displaying entered values in information box</span>
<span class="pln">zenity </span><span class="pun">--</span><span class="pln">info </span><span class="pun">--</span><span class="pln">title </span><span class="str">"Information"</span><span class="pun">--</span><span class="pln">text </span><span class="str">"You are ${FIRSTNAME} ${LASTNAME} and you are ${AGE}(s) old."</span><span class="pun">--</span><span class="pln">width</span><span class="pun">=</span><span class="lit">300</span><span class="pun">--</span><span class="pln">height</span><span class="pun">=</span><span class="lit">100</span>
这些是运行前面脚本的截图。
框1
输入框
输入框
输入框
信息框
别忘了查看也许能帮助到你的有用的zenity 选项。
Whiptail 工具
在Ubuntu上安装whiptail,运行
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install whiptail</span>
用whiptail创建消息框或者对话框的命令也是无需解释的,我们会给你提供一些基本例子作为参考。
创建消息框
<span class="pln">whiptail </span><span class="pun">--</span><span class="pln">msgbox </span><span class="str">"This is a message"</span><span class="lit">10</span><span class="lit">40</span>
创建 Yes/No 对话框
<span class="pln">whiptail </span><span class="pun">--</span><span class="pln">yes</span><span class="pun">-</span><span class="pln">button </span><span class="str">"Yeah"</span><span class="pun">--</span><span class="kwd">no</span><span class="pun">-</span><span class="pln">button </span><span class="str">"Nope"</span><span class="pun">--</span><span class="pln">title </span><span class="str">"Choose the answer"</span><span class="pun">--</span><span class="pln">yesno </span><span class="str">"Will you choose yes?"</span><span class="lit">10</span><span class="lit">30</span>
创建有缺省值的输入框
<span class="pln">whiptail </span><span class="pun">--</span><span class="pln">inputbox </span><span class="str">"Enter your number please."</span><span class="lit">10</span><span class="lit">30</span><span class="str">"10"</span>
尝试使用输入值要注意的一点是whiptail用stdout显示对话框,用stderr输出值。这样的话,如果你用 var=$(...),你就根本不会看到对话框,也不能获得输入的值。解决方法是交换stdout和stderr。在whiptail命令后面添加 3>&1 1>&2 2>&3 就可以做到。你想获取输入值的任何whiptail命令也是如此。
创建菜单对话框
<span class="pln">whiptail </span><span class="pun">--</span><span class="pln">menu </span><span class="str">"This is a menu. Choose an option:"</span><span class="lit">20</span><span class="lit">50</span><span class="lit">10</span><span class="lit">1</span><span class="str">"first"</span><span class="lit">2</span><span class="str">"second"</span><span class="lit">3</span><span class="str">"third"</span>
这是一个请求用户输入一个文件夹的路径并输出它的大小的 shell 脚本。
<span class="com">#!/bin/bash</span>
<span class="com">#</span>
<span class="com">#</span>
<span class="com"># Since whiptail has to use stdout to display dialog, entered value will</span>
<span class="com"># be stored in stderr. To switch them and get the value to stdout you must</span>
<span class="com"># use 3>&1 1>&2 2>&3</span>
<span class="pln">FOLDER_PATH</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="pln">whiptail </span><span class="pun">--</span><span class="pln">title </span><span class="str">"Get the size of folder"</span><span class="pln"> \</span>
<span class="pun">--</span><span class="pln">inputbox </span><span class="str">"Enter folder path:"</span><span class="pln"> \</span>
<span class="lit">10</span><span class="lit">30</span><span class="pln"> \</span>
<span class="str">"/home"</span><span class="pln"> \</span>
<span class="lit">3</span><span class="pun">>&</span><span class="lit">1</span><span class="lit">1</span><span class="pun">>&</span><span class="lit">2</span><span class="lit">2</span><span class="pun">>&</span><span class="lit">3</span><span class="pun">)</span>
<span class="kwd">if</span><span class="pun">[</span><span class="pun">-</span><span class="pln">d $FOLDER_PATH </span><span class="pun">]</span>
<span class="kwd">then</span>
<span class="pln">size</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="pln">du </span><span class="pun">-</span><span class="pln">hs </span><span class="str">"$FOLDER_PATH"</span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $1}'</span><span class="pun">)</span>
<span class="pln">whiptail </span><span class="pun">--</span><span class="pln">title </span><span class="str">"Information"</span><span class="pln"> \</span>
<span class="pun">--</span><span class="pln">msgbox </span><span class="str">"Size of ${FOLDER_PATH} is ${size}"</span><span class="pln"> \</span>
<span class="lit">10</span><span class="lit">40</span>
<span class="kwd">elif</span><span class="pun">[</span><span class="pun">-</span><span class="pln">f $FOLDER_PATH </span><span class="pun">]</span>
<span class="kwd">then</span>
<span class="pln">whiptail </span><span class="pun">--</span><span class="pln">title </span><span class="str">"Warning!!!"</span><span class="pln"> \</span>
<span class="pun">--</span><span class="pln">msgbox </span><span class="str">"The path you entered is a path to a file not a folder!"</span><span class="pln"> \</span>
<span class="lit">10</span><span class="lit">40</span>
<span class="kwd">else</span>
<span class="pln">whiptail </span><span class="pun">--</span><span class="pln">title </span><span class="str">"Error!!!"</span>
<span class="pun">--</span><span class="pln">msgbox </span><span class="str">"Path you entered is not recognized. Please try again"</span><span class="pln"> \</span>
<span class="lit">10</span><span class="lit">40</span>
<span class="kwd">fi</span>
这是之前例子的一些截图:
输入框
消息框
如果你在终端下工作,帮助手册总是有用的。
结论
选择合适的工具显示对话框取决于你期望在桌面机器还是服务器上运行你的脚本。桌面机器用户通常使用GUI窗口环境,也可能运行脚本并与显示的窗口进行交互。然而,如果你期望用户是在服务器上工作的,(在没有图形界面时,)你也许希望能确保总能显示,那就使用whiptail或者任何其它在纯终端窗口显示对话框的工具。
via: http://linoxide.com/linux-shell-script/bash-shell-script-show-dialog-box/
作者:Ilija Lazarevic 译者:ictlyh 校对:wxy