Use Zenity to GUI-Up Scripts

Some people just aren't comfortable with the command line. If you're writing scripts for those people, you want to take a look at Zenity, a GNOME utility that provides simple GUI dialogs from a shell script.

Not everyone is comfortable with a command-line interface. If you're writing scripts for users who prefer to work from a GUI, check out Zenity, a GNOME utility that provides simple GUI dialogs from a shell script.

As a system administrator, you must not only come up with scripts that do the job -- but also keep users happy. One way to do so is to ensure some users have as little contact with the command-line interface (CLI) as possible. If you're fortunate enough to have users who are working with Linux desktops (like Ubuntu) then Zenity provides a toolkit for dialogs to gather user input and make it look like you've done much more coding than you really have.

A simple example: Say you have a script that helps a user back up some data. All he or she must do is specify the directory to be backed up and to where. Explaining how to navigate the CLI to get the right directories and ensuring they are typed correctly can be less than pleasant for both parties involved. Lest we forget, most users don't find something like /home/user/Documents to be intuitive or friendly -- especially if they're used to doing everything on Windows or Mac OS X.

Hence, a text-mode script that prompts the user to type in something like that is going to be unpopular. Instead, use Zenity to let your users point and click their way to happiness (and data backups). In the script where you need a path input, use zenity --file-selection --directory instead. This will pop up a Nautilus file selector. When the user selects a directory, it will simply be returned to the shell script as the path -- and easily digestible by the script.

Just want a file and not a whole directory? Omit the --directory option, and you'll have a standard file dialog.

You can pass a title or other text to the dialogs, of course. For example, zenity --warning --title='Warning! This is Permanent!' will display a warning dialog before proceeding. Note that must escape the exclamation points with single quotes. Of course, you might want to use a --question dialog instead.

Actually, Zenity has quite a few dialog options. It offers a calendar, text entry, info, list dialog (for choosing from a list), notification dialog, progress dialog and so on. The dialogs can be controlled by height and width, as well as the option of a timeout -- so a warning dialog will not be indefinitely displayed, for example.

All in all, Zenity can be extremely useful if you want to boost your shell scripts' power. The only complaint I have with Zenity isn't how it works -- it's the paucity of the documentation that comes with it. The man page is a decent reference, but it doesn't give many examples. Aside from that, Zenity is a powerful tool for any admin that needs a quick and dirty way to add a GUI to her scripts. Take an afternoon and give it a spin -- you might be surprised by how much you can accomplish.

Joe 'Zonker' Brockmeier is a freelance writer and editor with more than 10 years covering IT. Formerly the openSUSE Community Manager for Novell, Brockmeier has written for Linux Magazine, Sys Admin, Linux Pro Magazine, IBM developerWorks, Linux.com, CIO.com, Linux Weekly News, ZDNet, and many other publications. You can reach Zonker at jzb@zonker.net and follow him on Twitter.

Follow ServerWatch on Twitter

This article was originally published on Feb 28, 2011
Page 1 of 1

Thanks for your registration, follow us on our social networks to keep up-to-date