Data Centers

GUI scripting in bash

GUI programming for shell scripts, with helper programs like Zenity and kdialog, is much easier to accomplish than GUI programs for other programming environments. Vincent Danen demonstrates what you can do with them.

When it comes to writing custom applications for servers or desktops on Linux, there are a number of choices of languages to use. You can write apps in python, perl, ruby, or even php, among so many others. Scripts can be written in the shell as well, using bash, zsh, or tcsh, to name a few. Out of all of these, writing scripts in the shell (and bash comes with pretty much every Linux distribution out there), is perhaps the easiest to pick up and the quickest to write.

The nice thing about a shell script is that, in itself, there isn't much to do as far as programming. Shell scripts are really just a sequence of commands you would write on the command line. It does support if-then-else type statements; it supports loops; and it can become quite complex if you want it to be, but it doesn't have to be.

And, like other languages, shell scripts can have GUI front-ends as well. There are a few different choices here: Zenity for the GNOME desktop, kdialog for the KDE desktop, and dialog for a TUI, providing menus and prompts suitable for the CLI. To put the power of shell scripts into a little perspective, I wrote the entire Annvix operating system install program in bash, using dialog to ask questions about the installation.

With Zenity and kdialog, you can write scripts that prompt users for input using GUI windows. For instance, to ask the user to answer a question with "Yes" or "No" you would use:

zenity —question "Do you wish to proceed?"
rc=$?
if [ "${rc}" == "0" ]; then
    answer="yes"
else
    answer="no"
fi

and the equivalent in kdialog:

kdialog —yesno "Do you wish to proceed?"
rc=$?
if [ "${rc}" == "0" ]; then
    answer="yes"
else
    answer="no"
fi

Both Zenity and kdialog will display further information with the —help option, and Zenity has a manpage that can be referenced for help.

Another example is in creating a progress bar. For instance, to show a progress bar while finding all backup files in your home directory, you might use:

find . -name '*~' | tee >(zenity —progress —pulsate —title "Finding backup files" —text "Searching for backup files...") >out.txt
zenity —width 400 —height 100 —title "File list" —text-info —filename=out.txt

kdialog works a little strangely here, so the above example doesn't work so well since find only works in one run, but to illustrate with a little sleep trickery:

ref=$(kdialog —progressbar "Finding backup files" 3)
qdbus ${ref} org.kde.kdialog.ProgressDialog.setLabelText "Searching for backup files..."
qdbus ${ref} Set org.kde.kdialog.ProgressDialog value 1
find . -name '*~' >out.txt
qdbus ${ref} Set org.kde.kdialog.ProgressDialog value 2
sleep 2
qdbus ${ref} Set org.kde.kdialog.ProgressDialog value 3
qdbus ${ref} org.kde.kdialog.ProgressDialog.close
kdialog —textbox out.txt 400 100

Both will display a progress bar (quite short lived, for this example) and then will display the output of the find command (which was redirected to a file called out.txt). Note that kdialog on KDE3 is different than it is on KDE4 (the above is for KDE4). If you are still using KDE3, communication is done via DCOP, rather than DBUS.

These illustrations are quite simple, but they demonstrate the possibilities of what can be done with either tool. Out of the two, I find Zenity to be much simpler to work with, but they both work quite well, and it of course depends upon your environment of choice.

GUI programming for shell scripts, with helper programs like Zenity and kdialog, is much easier to accomplish than GUI programs for other programming environments. If you need to write a program that requires user interaction, and you also need that interaction to be via the desktop and not a terminal or shell, then these tools can help realize that goal.

Get the PDF version of this tip here.

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

Editor's Picks