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

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

日期:2017/3/1 9:28:51   编辑:SHELL編程

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

Zenity 工具

在Ubuntu中安裝zenity,運行:

  1. sudo apt-get install zenity

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

創建消息框

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

創建 Yes/No 詢問對話框

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

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

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

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

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

  1. #!/bin/bash
  2. #
  3. # This script will ask for couple of parameters
  4. # and then continue to work depending on entered values
  5. #
  6. # Giving the option to user
  7. zenity --question --text "Do you want to continue?"
  8. # Checking if user wants to proceed
  9. [ $?-eq 0]||exit1
  10. # Letting user input some values
  11. FIRSTNAME=$(zenity --entry --title "Entry box"--text "Please, enter your first name."--width=300--height=150)
  12. LASTNAME=$(zenity --entry --title "Entry box"--text "Please, enter your last name."--width=300--height=150)
  13. AGE=$(zenity --entry --title "Entry box"--text "Please, enter your age."--width=300--height=150)
  14. # Displaying entered values in information box
  15. zenity --info --title "Information"--text "You are ${FIRSTNAME} ${LASTNAME} and you are ${AGE}(s) old."--width=300--height=100

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

框1

輸入框

輸入框

輸入框

信息框

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

Whiptail 工具

在Ubuntu上安裝whiptail,運行

  1. sudo apt-get install whiptail

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

創建消息框

  1. whiptail --msgbox "This is a message"1040

創建 Yes/No 對話框

  1. whiptail --yes-button "Yeah"--no-button "Nope"--title "Choose the answer"--yesno "Will you choose yes?"1030

創建有缺省值的輸入框

  1. whiptail --inputbox "Enter your number please."1030"10"

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

創建菜單對話框

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

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

  1. #!/bin/bash
  2. #
  3. #
  4. # Since whiptail has to use stdout to display dialog, entered value will
  5. # be stored in stderr. To switch them and get the value to stdout you must
  6. # use 3>&1 1>&2 2>&3
  7. FOLDER_PATH=$(whiptail --title "Get the size of folder" \
  8. --inputbox "Enter folder path:" \
  9. 1030 \
  10. "/home" \
  11. 3>&11>&22>&3)
  12. if[-d $FOLDER_PATH ]
  13. then
  14. size=$(du -hs "$FOLDER_PATH"| awk '{print $1}')
  15. whiptail --title "Information" \
  16. --msgbox "Size of ${FOLDER_PATH} is ${size}" \
  17. 1040
  18. elif[-f $FOLDER_PATH ]
  19. then
  20. whiptail --title "Warning!!!" \
  21. --msgbox "The path you entered is a path to a file not a folder!" \
  22. 1040
  23. else
  24. whiptail --title "Error!!!"
  25. --msgbox "Path you entered is not recognized. Please try again" \
  26. 1040
  27. fi

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

輸入框

消息框

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

結論

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


via: http://linoxide.com/linux-shell-script/bash-shell-script-show-dialog-box/

作者:Ilija Lazarevic 譯者:ictlyh 校對:wxy

本文由 LCTT 原創翻譯,Linux中國 榮譽推出

Copyright © Linux教程網 All Rights Reserved