歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> 如何在 Bash Shell 腳本中顯示對話框

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

日期:2017/3/2 9:45:21   编辑:SHELL編程

這個教程給出幾個如何使用類似zenity和whiptail的工具在Bash Shell 腳本中提供消息/對話框的例子。使用這些工具,你的腳本能夠告知用戶當前程序運行的狀態並能與用戶進行交互。這兩個工具的不同之處在於顯示消息框或者對話框的方式。Zenity用GTK工具包創建圖形用戶界面,而whiptail則在終端窗口內創建消息框。

Zenity 工具

在Ubuntu中安裝zenity,運行:

sudo apt-get install zenity

用zenity創建消息框或者對話框的命令是不言自明的,我們會給你提供一些例子來參考。

創建消息框

zenity --info --title "Information Box" --text "This should be information" --width=300 --height=200

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

創建 Yes/No 詢問對話框

zenity --question --text "Do you want this?" --ok-label "Yeah" --cancel-label="Nope"

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

創建輸入框並將輸入值保存到變量中

a=$(zenity --entry --title "Entry box" --text "Please enter the value" --width=300 --height=200)
echo $a

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

輸入後,值會保存在變量 $a 中。

這是一個獲取用戶姓名並顯示的實際事例。

#!/bin/bash
#
# This script will ask for couple of parameters
# and then continue to work depending on entered values
#

# Giving the option to user
zenity --question --text "Do you want to continue?"

# Checking if user wants to proceed
[ $? -eq 0 ] || exit 1

# Letting user input some values
FIRSTNAME=$(zenity --entry --title "Entry box" --text "Please, enter your first name." --width=300 --height=150)
LASTNAME=$(zenity --entry --title "Entry box" --text "Please, enter your last name." --width=300 --height=150)
AGE=$(zenity --entry --title "Entry box" --text "Please, enter your age." --width=300 --height=150)

# Displaying entered values in information box
zenity --info --title "Information" --text "You are ${FIRSTNAME} ${LASTNAME} and you are ${AGE}(s) old." --width=300 --height=100

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

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

框1

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

輸入框

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

輸入框

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

輸入框

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

信息框

別忘了查看也許能幫助到你的有用的zenity 選項。

Whiptail 工具

在Ubuntu上安裝whiptail,運行

sudo apt-get install whiptail

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

創建消息框

whiptail --msgbox "This is a message" 10 40

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

創建 Yes/No 對話框

whiptail --yes-button "Yeah" --no-button "Nope" --title "Choose the answer" --yesno "Will you choose yes?" 10 30

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

創建有缺省值的輸入框

whiptail --inputbox "Enter your number please." 10 30 "10"

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

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

創建菜單對話框

whiptail --menu "This is a menu. Choose an option:" 20 50 10 1 "first" 2 "second" 3 "third"

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

這是一個請求用戶輸入一個文件夾的路徑並輸出它的大小的 shell 腳本

#!/bin/bash
#
#

# Since whiptail has to use stdout to display dialog, entered value will
# be stored in stderr. To switch them and get the value to stdout you must
# use 3>&1 1>&2 2>&3
FOLDER_PATH=$(whiptail --title "Get the size of folder" /
--inputbox "Enter folder path:" /
10 30 /
"/home" /
3>&1 1>&2 2>&3)

if [ -d $FOLDER_PATH ]
then
size=$(du -hs "$FOLDER_PATH" | awk '{print $1}')
whiptail --title "Information" /
--msgbox "Size of ${FOLDER_PATH} is ${size}" /
10 40
elif [ -f $FOLDER_PATH ]
then
whiptail --title "Warning!!!" /
--msgbox "The path you entered is a path to a file not a folder!" /
10 40
else
whiptail --title "Error!!!"
--msgbox "Path you entered is not recognized. Please try again" /
10 40
fi

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

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

輸入框

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

消息框

如果你在終端下工作,幫助手冊總是有用的。

結論

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

Copyright © Linux教程網 All Rights Reserved