Zenity: Shell Scripting with Gnome

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search

Install zenity command

Type the following command to install the KDialog command line utility under Debian or Ubuntu Linux:

sudo apt-get update
sudo apt-get install zenity

Type the following command to install the dialog command line utility under CentOS, Fedora or Redhat Linux:

yum install zenity

Syntax

The syntax is as follows:

zenity --common-options --dialog-type --dialog-specific-options

Where,

  • --common-options : All Zenity dialogs support the following general options:
    • --title="Your Title Text" : Set the title of a dialog.
    • --window-icon=/path/to/icon_path : Set the icon that is displayed in the window frame of the dialog. There are four stock icons also available by providing the following keywords - 'info', 'warning', 'question' and 'error'.
    • --width=width : Set the width of the dialog.
    • --height=height : Set the height of the dialog.
    • --timeout=timeout : Set the timeout in seconds after which the dialog is closed.
  • --dialog-type : Set the dialog box type such as calendar or error box.
  • --dialog-specific-options are used to set dialog boxes title, and other information

Your first GUI dialog using zenity

zenity command in action

Type the following command at a shell prompt:

zenity --title "File not found"  --error --text="Could not find /var/log/rsnapshot log file."
  1. The above example shows how to create an error dialog with a single OK button.
  2. You can use this dialog box to display error messages.
  3. After reading the message, the user can press the ENTER key so that dialog will exit and the calling shell script can continue its operation.
  4. If the message is too large for the space, zenity may wrap the message.

Quotation marks

When you use the [[[zenity command]], make sure that you place quotation marks around each argument. For example, use:

zenity --title "File not found"  --error --text="Could not find /var/log/rsnapshot log file."

Do not use (note missing quotation marks):

zenity --title File not found"  --error --text="Could not find /var/log/rsnapshot log file.

If you do not use quotation marks, you might get unexpected results.

Single quotation marks

The single quote ( 'quote' ) protects everything enclosed between two single quote marks. It is used to turn off the special meaning of all characters. For example, use:

zenity --title='Birthday Planner!' --calendar

Do not use double quote:

zenity --title="Birthday Planner!" --calendar

Sample outputs:

bash: !": event not found

Common zenity options

Usage:
  zenity [OPTION...]

Help Options:
  -h, --help                                     Show help options
  --help-all                                     Show all help options
  --help-general                                 Show general options
  --help-calendar                                Show calendar options
  --help-entry                                   Show text entry options
  --help-error                                   Show error options
  --help-info                                    Show info options
  --help-file-selection                          Show file selection options
  --help-list                                    Show list options
  --help-notification                            Show notification icon options
  --help-progress                                Show progress options
  --help-question                                Show question options
  --help-warning                                 Show warning options
  --help-scale                                   Show scale options
  --help-text-info                               Show text information options
  --help-misc                                    Show miscellaneous options
  --help-gtk                                     Show GTK+ Options

Application Options:
  --calendar                                     Display calendar dialog
  --entry                                        Display text entry dialog
  --error                                        Display error dialog
  --info                                         Display info dialog
  --file-selection                               Display file selection dialog
  --list                                         Display list dialog
  --notification                                 Display notification
  --progress                                     Display progress indication dialog
  --question                                     Display question dialog
  --warning                                      Display warning dialog
  --scale                                        Display scale dialog
  --text-info                                    Display text information dialog
  --display=DISPLAY                              X display to use