mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
[Translated] tech/20150413 How to Show Dialog Box from Bash Shell Script with Examples
This commit is contained in:
parent
a62a8ab815
commit
ec7ecd6603
@ -1,175 +0,0 @@
|
||||
Translating by ictlyh
|
||||
How to Show Dialog Box from Bash Shell Script with Examples
|
||||
================================================================================
|
||||
This tutorial will give couple of examples on how to use utilities like zenity and whiptail in your Bash shell script to provide message / dialog box . With these utilities your script will be able to inform user about the current state of execution, or give an ability to interact. Difference between these two utilities is the way they are displaying message box or dialog. Zenity uses GTK toolkit for creating graphical user interfaces, while whiptail creates message boxes inside terminal window.
|
||||
|
||||
### Zenity Tool ###
|
||||
|
||||
To install zenity on Ubuntu run:
|
||||
|
||||
sudo apt-get install zenity
|
||||
|
||||
Since commands for creating message boxes or dialogs with zenity are pretty self explanatory, we will provide you with couple of examples.
|
||||
|
||||
### Creating information box ###
|
||||
|
||||
zenity --info --title "Information Box" --text "This should be information" --width=300 --height=200
|
||||
|
||||
![Screenshot-Information Box](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-Information-Box.png)
|
||||
|
||||
Creating Yes/No dialog
|
||||
|
||||
zenity --question --text "Do you want this?" --ok-label "Yeah" --cancel-label="Nope"
|
||||
|
||||
![Screenshot-Question](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-Question.png)
|
||||
|
||||
Creating entry box and storing value in variable
|
||||
|
||||
a=$(zenity --entry --title "Entry box" --text "Please enter the value" --width=300 --height=200)
|
||||
echo $a
|
||||
|
||||
![Screenshot-Entry box](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-Entry-box.png)
|
||||
|
||||
After entering it, value will be stored in $a variable.
|
||||
|
||||
Here is the working example which takes user's first name, last name and age and displays it.
|
||||
|
||||
#!/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
|
||||
|
||||
Here are the screenshots of previous script.
|
||||
|
||||
![Example1-Question-1](http://blog.linoxide.com/wp-content/uploads/2015/03/Example1-Question-1.png)
|
||||
|
||||
First box
|
||||
|
||||
![Example1-Entry box-1](http://blog.linoxide.com/wp-content/uploads/2015/03/Example1-Entry-box-1.png)
|
||||
|
||||
Entry box
|
||||
|
||||
![Example1-Entry box-2](http://blog.linoxide.com/wp-content/uploads/2015/03/Example1-Entry-box-2.png)
|
||||
|
||||
Entry box
|
||||
|
||||
![Example1-Entry box-3](http://blog.linoxide.com/wp-content/uploads/2015/03/Example1-Entry-box-3.png)
|
||||
|
||||
Entry box
|
||||
|
||||
![Example1-Information](http://blog.linoxide.com/wp-content/uploads/2015/03/Example1-Information.png)
|
||||
|
||||
Information box
|
||||
|
||||
Don't forget refer some of the useful [zenity options][1] that might help you.
|
||||
|
||||
### Whiptail Tool ###
|
||||
|
||||
To install whiptail on Ubuntu run
|
||||
|
||||
sudo apt-get install whiptail
|
||||
|
||||
Commands for creating message box/dialog with whiptail are also self explanatory, so we will provide you with just couple of basic examples.
|
||||
|
||||
### Creating message box ###
|
||||
|
||||
whiptail --msgbox "This is a message" 10 40
|
||||
|
||||
![Screenshot-whiptail-msgbox](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-whiptail-msgbox.png)
|
||||
|
||||
### Creating Yes/No dialog ###
|
||||
|
||||
whiptail --yes-button "Yeah" --no-button "Nope" --title "Choose the answer" --yesno "Will you choose yes?" 10 30
|
||||
|
||||
![Screenshot-whiptail-yesno](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-whiptail-yesno.png)
|
||||
|
||||
### Creating entry box with default value ###
|
||||
|
||||
whiptail --inputbox "Enter your number please." 10 30 "10"
|
||||
|
||||
![Screenshot-whiptail-entry](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-whiptail-entry.png)
|
||||
|
||||
One thing to be aware of when trying to use entered value is that whiptail uses stdout for displaying dialog, and stderr for value output. That way, if you use var=$(...) you wont see dialog box at all, and wont get the entered value. Solution is to switch stdout and stderr. To do that just add **3>&1 1>&2 2>&3** at the end of the whiptail command. Same would be with any whiptail command which you want to use to get some entered value.
|
||||
|
||||
### Creating menu dialog ###
|
||||
|
||||
whiptail --menu "This is a menu. Choose an option:" 20 50 10 1 "first" 2 "second" 3 "third"
|
||||
|
||||
![Screenshot-whiptail-menu](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-whiptail-menu.png)
|
||||
|
||||
Here is a **shell script** that asks user to enter a path to a folder and then outputs it's size.
|
||||
|
||||
#!/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
|
||||
|
||||
Here are the screenshots from previous example:
|
||||
|
||||
![Example2-Entry-box](http://blog.linoxide.com/wp-content/uploads/2015/03/Example2-Entry-box.png)
|
||||
|
||||
Entry box
|
||||
|
||||
![Example2-Information](http://blog.linoxide.com/wp-content/uploads/2015/03/Example2-Information.png\)
|
||||
|
||||
Information box
|
||||
|
||||
If you are working in terminal , [manual pages][2] are always available.
|
||||
|
||||
### Conclusion ###
|
||||
|
||||
Choosing the right tool for displaying dialogs will depend on whether you expect your script to be run on desktop machine or server machine. Desktop machine users mostly use window environment and will possibly be able to run the script and interact with appearing windows. However, if you are expecting that the user is someone on server machine, you might want to play it safe and use whiptail or any other utility that will display dialogs in plain terminal window.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://linoxide.com/linux-shell-script/bash-shell-script-show-dialog-box/
|
||||
|
||||
作者:[Ilija Lazarevic][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://linoxide.com/author/ilijala/
|
||||
[1]:https://help.gnome.org/users/zenity/stable/
|
||||
[2]:http://linux.die.net/man/1/whiptail
|
@ -0,0 +1,174 @@
|
||||
如何在Bash 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
|
||||
|
||||
![消息框截图](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-Information-Box.png)
|
||||
|
||||
创建 Yes/No 对话框
|
||||
|
||||
zenity --question --text "Do you want this?" --ok-label "Yeah" --cancel-label="Nope"
|
||||
|
||||
![问题截图](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-Question.png)
|
||||
|
||||
创建输入框并在变量中保存值
|
||||
|
||||
a=$(zenity --entry --title "Entry box" --text "Please enter the value" --width=300 --height=200)
|
||||
echo $a
|
||||
|
||||
![输入框截图](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-Entry-box.png)
|
||||
|
||||
输入后,值会保存在变量 $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
|
||||
|
||||
这些是运行前面脚本的截图。
|
||||
|
||||
![例1-问题-1](http://blog.linoxide.com/wp-content/uploads/2015/03/Example1-Question-1.png)
|
||||
|
||||
框1
|
||||
|
||||
![例1-输入框-1](http://blog.linoxide.com/wp-content/uploads/2015/03/Example1-Entry-box-1.png)
|
||||
|
||||
输入框
|
||||
|
||||
![例1-输入框-2](http://blog.linoxide.com/wp-content/uploads/2015/03/Example1-Entry-box-2.png)
|
||||
|
||||
输入框
|
||||
|
||||
![例1-输入框-3](http://blog.linoxide.com/wp-content/uploads/2015/03/Example1-Entry-box-3.png)
|
||||
|
||||
输入框
|
||||
|
||||
![例1-信息](http://blog.linoxide.com/wp-content/uploads/2015/03/Example1-Information.png)
|
||||
|
||||
信息框
|
||||
|
||||
别忘了查看也许能帮助到你的有用的[zenity 选项][1]
|
||||
|
||||
### Whiptail 工具 ###
|
||||
|
||||
在Ubuntu上安装whiptail,运行
|
||||
|
||||
sudo apt-get install whiptail
|
||||
|
||||
用whiptail创建消息框或者对话框的命令也是相当自解释的,我们会给你提供一些基本例子。
|
||||
|
||||
### 创建消息框 ###
|
||||
|
||||
whiptail --msgbox "This is a message" 10 40
|
||||
|
||||
![whiptail消息框截图](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-whiptail-msgbox.png)
|
||||
|
||||
### 创建 Yes/No 对话框 ###
|
||||
|
||||
whiptail --yes-button "Yeah" --no-button "Nope" --title "Choose the answer" --yesno "Will you choose yes?" 10 30
|
||||
|
||||
![whiptail对话框截图](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-whiptail-yesno.png)
|
||||
|
||||
### 创建有缺省值的输入框 ###
|
||||
|
||||
whiptail --inputbox "Enter your number please." 10 30 "10"
|
||||
|
||||
![whiptail输入框截图](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-whiptail-entry.png)
|
||||
|
||||
尝试使用输入值要注意的一点是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"
|
||||
|
||||
![whiptail菜单截图](http://blog.linoxide.com/wp-content/uploads/2015/03/Screenshot-whiptail-menu.png)
|
||||
|
||||
这是一个请求用户输入一个文件夹的路径并输出它的大小的 **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
|
||||
|
||||
这是之前例子的一些截图:
|
||||
|
||||
![例2-输入框](http://blog.linoxide.com/wp-content/uploads/2015/03/Example2-Entry-box.png)
|
||||
|
||||
输入框
|
||||
|
||||
![例2-消息框](http://blog.linoxide.com/wp-content/uploads/2015/03/Example2-Information.png\)
|
||||
|
||||
消息框
|
||||
|
||||
如果你在终端下工作,[帮助手册][2]总是有用的。
|
||||
|
||||
### 结论 ###
|
||||
|
||||
选择合适的工具显示对话框取决于你期望在桌面机器还是服务器上运行你的脚本。桌面机器用户通常使用窗口环境,更可能用显示的窗口运行脚本和交互。然而,如果你期望用户是在服务器上工作的,你也许会希望安全地显示,并使用whiptail或者任何其它在纯终端窗口显示对话框的工具。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://linoxide.com/linux-shell-script/bash-shell-script-show-dialog-box/
|
||||
|
||||
作者:[Ilija Lazarevic][a]
|
||||
译者:[ictlyh](https://github.com/ictlyh)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://linoxide.com/author/ilijala/
|
||||
[1]:https://help.gnome.org/users/zenity/stable/
|
||||
[2]:http://linux.die.net/man/1/whiptail
|
Loading…
Reference in New Issue
Block a user