Every system administrator needs to use shell scripts from time to time to automate the more mundane tasks. While...
many of these little helpers work just fine from the command line, there are times when some user input is required. You could use a full-fledged programming language like Python along with something like TCK/TK, or you could use Zenity. Zenity is based on the GTK+ windowing toolkit and gives the administrator access to a number of useful user input and output widgets from the command line and, hence, a shell script. While the list is somewhat limited, it does provide things like a date picker, a file/directory chooser, list boxes, message boxes and a progress bar. Zenity is not directly scriptable but does provide results to standard output to include in your scripts. In this Zenity tutorial, we’ll walk through a number of practical examples of how you might use Zenity to put a pretty face on that crusty backup script you’ve been using. Getting started with Zenity A quick Google search will turn up a number of Zenity tutorials and a link to the Zenity Manual on gnome.org. The best way to get a feel for what you can do with Zenity is to open up a terminal session and give it a spin. Zenity has built-in help using the --help option. That gives you a list of all the options, including additional help for each of the different dialogs. The primary list of dialogs includes calendar, entry, error, info, file-selection, list, notification, progress, question, warning, scale and text-info.
Many of the dialogs have a simple set of options, such as the text entry dialog. Options include --entry- text to set the default text, --text to set the text shown above the entry box and --hide- text to mask the input as you would when entering a password. If you simply enter the command in a terminal window, you’ll see the dialog appear, and then the result will be echoed to the screen. An example using the --entry command could look something like the following:
$ zenity --entry --text=”Please enter your name”
There will be times when you want to take different actions based on what the user enters, such as if the cancel button is clicked. This type of interaction will require a little shell scripting to capture the output of the dialog, but isn’t complicated. If you’re not familiar with shell or Bash scripting, you’ll want to take a look at any of the really good tutorials on the Web including this Bash programming introduction. If you prefer paper books, you might want to try the latest edition of the Unix and Linux System Administration Handbook. It has an entire chapter on scripting and the shell along with 31 other chapters with everything you ever needed to know to be a Linux system admin.
Files and directories
One of the more common tasks in scripting is the need to select a file or a directory to use in performing some task. The Zenity file selection dialog provides access to the standard GTK+ file dialog and outputs the full path to the selected file as the output. This dialog has a number of options including --confirm-overwrite for use in conjunction with --save to prompt if a use selects an existing file, which would result in overwriting the current file.
The following line of script will present a file-chooser dialog and store the result in the variable strFileName:
$ strFileName=$(zenity --file-selection --save --confirm-overwrite); echo $strFileName
The ‘$’ after the ‘=’ causes Bash to substitute the output from the Zenity command as the target of the assignment to the variable named strFileName. The result is then echoed to the terminal screen with the leading ‘$’ directing Bash to substitute the variable strFileName as the argument to the echo command.
Other options for the file-selection dialog include --multiple to allow multi-file selection, --file-filter for setting a filter of the files to be displayed and --directory for activating directory-only mode. It’s easy to see how this dialog could be chained to from a text-entry dialog to first choose a filter pattern before displaying the file-selection dialog. When using the --multiple option you can also use the --separator option to set what character will separate the file names. If you don’t, the ‘|’ will be used by default.
With a little experimentation, you should get an idea of how to insert Zenity dialogs into your existing scripts or write new ones. Any script that requires user input would be a good target along with the need to display output using a standard list box. The --list option makes a great solution if you need to present a list of information from which a user can select . It has many of the same options as the file-selection dialog in addition to --checklist and --radiolist to set the first column to either check boxes or radio buttons.
Here’s a quick one-line file tool to search for .log files and display them in a listbox dialog for selection:
x=$(find / -name “*.log” |zenity --list --column “Delete” --column “Files” --checklist --height 600 --width 600 --separator=”,”); echo $x
Zenity is definitely worth the time and effort to learn how to use it effectively. Once you get started, you won’t regret it.