Zenity Brings a Little GUI Goodness to Linux Shell Scripts - page 2
Dressing up Find
We'll need to do a little bash programming to show off some of Zenity's more powerful capabilities. When you cascade multiple dialog boxes, you want to make it possible for a user to cancel the whole process when the "Cancel" button is clicked. This is done through the use of a special variable ($?) that Zenity sets when each dialog box exits. To check for the cancel button you must add this code after each call to Zenity:
The result codes include 1 for when the user clicks cancel, 0 for when the user clicks OK or close, -1 for an unexpected error and 5 to signify that the dialog timeout value has been reached. This technique lets you do things like prompt for a string of text or take the output of a dialog like the file selector and pass it on to another step in the process. To save the text returned from either the file selection, list box or text entry box you need to provide a variable name as in the following example:
strFilePath=$(zenity --file-selection --save --confirm-overwrite);echo $strFilePath
The --timeout option will cause the dialog to close after specified number of seconds if no user input has been detected. This can be extremely useful if you don't want a dialog box to be left open indefinitely waiting on a user to click a button. This last example shows a script that will display all the .txt files in your documents directory and then launch gedit on the selected file:
strFilePath=$(zenity --file-selection --file-filter "*.txt");
if [ $? = 1 ];
With this simple script you could create an icon on the desktop that would pop up the file selector dialog box and quickly select a file to edit.
As a programmer, you sometimes have to make design decisions to complete your task, even when you're dealing with simple shell scripts. Creating simple utility scripts to automate tasks or make it easier to accomplish your work can really make you more productive. Zenity helps take some of the pain out of command line programming and makes it possible to create some cool apps even if they are just shell scripts.
Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.
- 1Linux Top 3: CoreOS, Oracle Enterprise Linux 7 and Ubuntu 14.10
- 2Linux Top 3: Debian Dumps SPARC, Ubuntu Takes Over Linux 3.13 and the Core Infrastructure Initiative
- 3Linux Top 3: Fedora, Ubuntu and Gluster Lose Community Leaders
- 4Red Hat Enterprise Linux 7 Finally Hits the Big Time
- 5Linux Top 3: Tails 1.0, OpenMandriva Lx 2014.0 and Debian 7.5