Linux

GUI scripting with Zenity

Vincent Danen introduces you to Zenity, a tool that allows you to create GUI prompts for shell scripts. Zenity creates GUI widgets and windows that can be used in a shell script.

One advantage to using Linux is that there is a plethora of powerful commands available on the command-line; however, many users prefer not to use the CLI or are intimidated by it. Some users simply prefer to use a GUI. With a program like Zenity, you can use both.

Zenity is a tool that allows you to create GUI prompts for shell scripts. Similar to the dialog tool, which creates nice "graphical" text interfaces for the CLI, Zenity creates GUI widgets and windows that can be used in a shell script.

For example, to simply have Zenity pop up an error window, you would use:

zenity --error --text="An error occurred\!" --title="Warning\!"

Here Zenity is told to create an error window with the --error command, that the text to be displayed in the window is "An error occurred!", and that the window's title should be "Warning!". Note that the "!" character must be escaped.

Using the --help argument will get a list of the various commands Zenity can take. For instance, you can use it to create a nice progress bar if you're doing something like a backup. This quick script illustrates its use:

#!/bin/sh
zenity --info --title="Demo Progress Script" --text="This script shows how Zenity creates a progress bar."
find /usr | zenity --progress --pulsate --auto-close --auto-kill --text="Working..."
zenity --info --title="Job complete" --text="The demo script is now complete."

This simple script illustrates the use of the progress bar. It will pop up the first window and pause for the user to hit OK then executes the find command and pipes the output into the Zenity progress bar, which is required for Zenity to know that something is happening. Finally, another informational window pops up when the task is complete.

Zenity can also take input from GUI windows, which can make it useful to ask questions of users. The --question command will display a window with text provided by the --text option. If the user presses OK then zenity returns an exit code of 0, otherwise an exit code of 1 is returned. In this way, it can be used to determine whether a script should proceed. For instance:

#!/bin/sh

zenity --question --text="Do you wish to continue/?"

rc=$?

if [ "${rc}" == "1" ]; then

echo "Program terminated."

exit 1

fi

echo "Proceeding"

Zenity can do many things. It can display a calendar and return the user's selected date, and it can show list dialogs or file selection dialogs. To show a calendar to have the user select a date, use:

#!/bin/sh
date=`zenity --calendar --date-format=%Y-%m-%d`
echo ${date}

To have the user select a file from a list, use something like:

#/bin/sh
cd ${HOME}
files=`find . | zenity --list --title "Select a file" --column "Files" --separator=" " --multiple`
for i in ${files}; do
    echo ${i}
done

This code finds all the files in the user's home directory and allows them to select one or more files, which are then written to the variable $files, which is then iterated over.

These are very short examples of what can be done with Zenity. The man page and some experimentation will really illustrate what can be done with Zenity to provide a GUI interface to shell scripts.

Get the PDF version of this tip here.

Delivered each Tuesday, TechRepublic's free Linux and Open Source newsletter provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

3 comments
p33d33
p33d33

Useful post, I've found a few good uses for it already! :) Keep up the good work. P

The_Dog
The_Dog

cool... I used zenity to catalogue some files on my system. Thanks for the post.

billlinux
billlinux

I have several Zenity + scripts that I use for specific 'root' jobs that would normally be done on the command line with sudo. But ... N.B. To get zenity and sudo to cooperate you first must comment out the /etc/sudoers default ## Defaults requiretty Once that is done sudo as part of a Zenity script will work. The following is an example of a simple script: #! /bin/bash # Open Emacs as root at / directory # file name: RootEmacs sudo -K zenity --entry \ --title="Write root files" \ --text="Enter your user _password:" \ --entry-text "" \ --hide-text |sudo -S emacs --no-splash / if [ "$?" != 0 ]; then zenity --error --text="Sorry, wrong password" exit 1 fi # End I launch RootEmacs from a desktop (or panel) launcher (icon). It is great for checking or changing configuration files on the fly.