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.
- 1Linux Top 3: Alpine Linux 3.4, deepin 15.2 and Linux Lite 3.0
- 2Linux 4.7 Set to Boost Live Patching, Security and Power Management
- 3Linux 4.6 Charred Weasel adds USB 3.1 Support
- 4Linux Top 3: OpenIndiana 2016.04, Ubuntu 16.04 and Debian's New Leader
- 5Linux Top 3: KaOS 2016.04, TurnKey 14.1 and pfSense 2.3