Disaster Recovery

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.

9 comments
LinuxCat
LinuxCat

It's a long complicated path but the end result is I need to find out how to use KDialog to actually copy a file that is selected from point A to point B. I can get the --getsavefilename to work in that the dialog appears, I can select the file in question change to a new location and click save, but all that happens is an output in konsole with no file actually being copied. So I am missing something but I don't know what.

sakurazuka
sakurazuka

A nice introduction. I have been searching, however, for a method of interacting with a windowed app via a bash script. So my question is - is it even possible? Thanks

fdhdfgh
fdhdfgh

input this URL: ( http://www.ashops.us) you can find many cheap and fashion stuff (jor dan s-h-o-e-s) (NBA NFL NHL MLB j-e-r-s-e-y) ( lv h-a-n-d-b-a-g) (cha nel w-a-l-l-e-t) (D&G s-u-n-g-l-a-s-s-e-s) (ed har dy j-a-c-k-e-t) (UG G b-o-o-t) WE ACCEPT PYAPAL PAYMENT YOU MUST NOT MISS IT!!!

fdhdfgh
fdhdfgh

input this URL: ( http://www.ashops.us) you can find many cheap and fashion stuff (jor dan s-h-o-e-s) (NBA NFL NHL MLB j-e-r-s-e-y) ( lv h-a-n-d-b-a-g) (cha nel w-a-l-l-e-t) (D&G s-u-n-g-l-a-s-s-e-s) (ed har dy j-a-c-k-e-t) (UG G b-o-o-t) WE ACCEPT PYAPAL PAYMENT YOU MUST NOT MISS IT!!!

Orodreth
Orodreth

Thanks very good tip. Definitely will be put to use.

lefty.crupps
lefty.crupps

I just, last night for the first time, started playing with KDialog and shell scripting. Very nice indeed! I was easily able to create a GUI to reload the wireless drivers on an older Acer Aspire laptop, making my wife's computer administration a bit easier. I found this thread to be very useful (English tho the URL is Spanish) http://www.wikilearning.com/articulo/shell_scripting_en_kde-kdialog_usage/193-4 but it was for KDE3 with DCOP. Your tips in this article has me thinking about new ways to implement my script, thank you so much!

David A. Pimentel
David A. Pimentel

Thanks for this tip. I do copious scripting on *nix machines, and I had not yet discovered these tools. I can see potential for creating much more user-friendly script widgets. I've discovered these tools on our Solaris systems as well. I found install instructions for MacOSX (http://zenity.darwinports.com) in case anyone is interested. It looks to to me that portability has been considered -- at least for modern *nix systems.

Jaqui
Jaqui

but not easy. you literally would have to capture the signals being sent to the windows and process them for content humanly readable to display it. most windowed apps on *x systems are just wrappers for cli apps. the most heavily used windowed apps aren't though. the wrapped cli apps are much easier to interface with, since they are designed to work with bash already.