November 1, 2014
 
 
RSSRSS feed

Zenity Brings a Little GUI Goodness to Linux Shell Scripts

Dressing up Find

  • September 3, 2009
  • By Paul Ferrill

One of the powers of the Linux command shell is the wealth of commands available to accomplish most any task. That amount of power can be intimidating to the less experienced users. Most typical computer users rarely open up a terminal window, or a DOS command box in Windows, unless they need to do some trouble shooting.

Adding a graphical user interface (GUI) component to any utility script is bound to make it more user friendly than a command-line only version. That's where Zenity comes in. Zenity is the command line interface to the GTK+ library of dialogs. It's a rewrite of the old gdialog utility but much more user friendly. The key to effectively using Zenity is to understand when and where to use it.

Zenity supports a wide variety of dialog boxes including calendar, file selection, list, messages, text entry and more. Direct control over the size of your dialogs comes by way of the <--width and --height options. You can even create a progress bar or a scale slider if you'd like. Official documentation on the latest stable release of Zenity can be found on the GNOME Documentation Library site.

Executing Zenity commands are pretty straightforward as long as you follow a few simple rules. Any time you need to enter text that is more than a single word you'll need to enclose the string in double quotes. This would be for things like the dialog title string or for an entry in a list box. The only other thing you need to know is that all Zenity options must be preceded by a double dash (--). You can get help on any Zenity option using the syntax --help-general, --help-list and so forth.

Dress up find

The find command is easily one of the handiest Linux commands but can also be one of the most frustrating. To search for all the picture files on your local drive you would type something like:

$ find . --name "*.jpg" --or --name "*.png"

Easy enough, but the result might not be what you want if you have lots of pictures. One quick way to add a little GUI list box to present the results in a more pleasant way would be as follows:

$ find . --name "*.jpg" --or --name "*.png" |zenity --list --column "Pictures"

You'll probably want to use the dimension modifiers to scale the list dialog to show more of the file name path. This would be done by adding these options to the zenity command:

--height 400 --width 400

A History GUI

Displaying the history of shell commands is a simple statement. If the list exceeds your terminal, you can just use the scroll bar to scan the entire list. Alternatively, you could use Zenity to present your history in a list box with a single line command:

$ history|cut -c 8-|zenity --list --column History

This line pipes the output of the history command to the cut command, which removes the first 7 characters of each line and then pipes the result into Zenity.

To make this little utility available in a shortened version you could use the alias command as in:

$ alias zh 'history|cut -c 8-|zenity --list --column History'

Sitemap | Contact Us