天天看點

如何在Bash Shell腳本中顯示對話框

這個教程給出幾個如何使用類似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>

如何在Bash Shell腳本中顯示對話框

消息框截圖

建立 yes/no 詢問對話框

<code>zenity --question --text "do you want this?" --ok-label "yeah" --cancel-label="nope"</code>

如何在Bash Shell腳本中顯示對話框

問題截圖

建立輸入框并将輸入值儲存到變量中

<code>a=$(zenity --entry --title "entry box" --text "please enter the value" --width=300 --height=200)</code>

<code>echo $a</code>

如何在Bash Shell腳本中顯示對話框

輸入框截圖

輸入後,值會儲存在變量 $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>

這些是運作前面腳本的截圖。

如何在Bash Shell腳本中顯示對話框

例1-問題-1

框1

如何在Bash Shell腳本中顯示對話框

例1-輸入框-1

輸入框

如何在Bash Shell腳本中顯示對話框

例1-輸入框-2

如何在Bash Shell腳本中顯示對話框

例1-輸入框-3

如何在Bash Shell腳本中顯示對話框

例1-資訊

資訊框

在ubuntu上安裝whiptail,運作

<code>sudo apt-get install whiptail</code>

用whiptail建立消息框或者對話框的指令也是無需解釋的,我們會給你提供一些基本例子作為參考。

<code>whiptail --msgbox "this is a message" 10 40</code>

如何在Bash Shell腳本中顯示對話框

whiptail消息框截圖

<code>whiptail --yes-button "yeah" --no-button "nope" --title "choose the answer" --yesno "will you choose yes?" 10 30</code>

如何在Bash Shell腳本中顯示對話框

whiptail對話框截圖

<code>whiptail --inputbox "enter your number please." 10 30 "10"</code>

如何在Bash Shell腳本中顯示對話框

whiptail輸入框截圖

嘗試使用輸入值要注意的一點是whiptail用stdout顯示對話框,用stderr輸出值。這樣的話,如果你用 var=$(...),你就根本不會看到對話框,也不能獲得輸入的值。解決方法是交換stdout和stderr。在whiptail指令後面添加 3&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;3 就可以做到。你想擷取輸入值的任何whiptail指令也是如此。

<code>whiptail --menu "this is a menu. choose an option:" 20 50 10 1 "first" 2 "second" 3 "third"</code>

如何在Bash Shell腳本中顯示對話框

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&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;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&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;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>

這是之前例子的一些截圖:

如何在Bash Shell腳本中顯示對話框

例2-輸入框

如何在Bash Shell腳本中顯示對話框

例2-消息框

消息框

選擇合适的工具顯示對話框取決于你期望在桌面機器還是伺服器上運作你的腳本。桌面機器使用者通常使用gui視窗環境,也可能運作腳本并與顯示的視窗進行互動。然而,如果你期望使用者是在伺服器上工作的,(在沒有圖形界面時,)你也許希望能確定總能顯示,那就使用whiptail或者任何其它在純終端視窗顯示對話框的工具。

原文釋出時間為:2015-06-02

本文來自雲栖社群合作夥伴“linux中國”