Interactive Scripts

Make Them Dynamic

Until now, we were reading what dialog says. It's time to make data flow the opposite direction. You send it to dialog via a pipe. This facilitates dynamic dialog contents, and there are several widgets that can benefit from it. Here, I'll cover two that I like most: You can find the rest in the man pages [3].

Suppose your script starts a time-consuming operation, such as creating a tarball from a large directory. It's a good idea to give the user some visual feedback of the execution's progress. As you know, tar -v prints filenames as it processes them. Why not reuse this handy feature?

The --programbox widget makes it a breeze. It reads lines of data from the standard input (unless you redefine this with --input-fd) and shows them in a dialog, scrolling as necessary. When the command finishes, the OK button appears so a user can review the output. Unfortunately, there is no support for scrolling at this point, which makes dialog --programbox a little less useful than it could be.

Try this:

tar czvf /dev/null smth/big | dialog --progressbox "Compressing..." 20 70

You'll see file names scrolling by quickly (as /dev/null is "fast and web-scale," you know), and the end result should look similar to Figure 5.

Figure 5: The --programbox widget is a way to display the program's output in a neat dialog box.

This sort of feedback is good, but it doesn't tell you how far the operation has already progressed. In other words, you might want percentages. dialog handles this as well via --gauge. It works in a similar fashion: You pipe integer values between 0 and 100, and dialog displays them along with a meter bar. There is also a way to change the text displayed on the widget in run time, so you can switch between "Installing files…" and "Finalizing the setup…" if you wish (again, the man pages [3] have more information).

This is not something we need for the next example, however. Calculating percentages could be tricky (and dialog doesn't help you there), so we'd opt for something more straightforward – a web download script (Figure 6). Tools like wget already do the math for us, and we can readily pump numbers into dialog. It is as easy as the pipeline shown in Listing 5.

Figure 6: Progress bars are great indicators of how far a long-running operation has progressed.

Listing 5

A Web Download Script

 

The first line invokes wget. The -q option tells it we just want a progress bar, and two other switches force a particular progress bar appearance. The wget command prints progress information to stderr, and we redirect it to a pipe.

Then comes sed, which is barely readable (as usual). I recommend turning off buffering with -u. This is needed so we get percentages one per line, one at the time. With buffering on, sed may send them in large batches, and the progress bar won't go smoothly. The -n option tells sed not to print any line unless explicitly requested. Then I look for lines having a percent sign embedded, delete everything but a number before the sign, and print the line. Note that this won't work for those HTTP servers that do not report file sizes; in which case, wget won't report any percentages.

Next, the values are piped into dialog. All options to --gauge should be self-explanatory. The only nuance is that you can specify the initial percentage via the fourth option.

The dialog tool (see also the "Dialog, the GUI Way" box) may not be as sophisticated as Qt or GTK+. On the other hand, you probably don't want to drive that complex interfaces from a shell script either. Keep dialog in mind if you are making multipurpose reusable shell scripts, which will interface with a human. Clicking on menus is much easier than remembering command-line options, even if you can do the latter.

Dialog, the GUI Way

All examples in this article were terminal-based, because dialog itself is a ncurses-based application. However, this doesn't mean you can't create graphical user interfaces in shell scripts. In fact, a few options are available.

For starters, there are Xdialog and gdialog. Both are a decade old yet retain a good degree of compatibility with dialog at the command-line syntax level. You are unlikely to find both in your package manager, so let's turn to newer alternatives.

If you are in the KDE camp, have a look at KDialog. It provides the same basic set of widgets as dialog, but the syntax is somewhat different. As this is a graphical, KDE-based application, it relies on a higher-level IPC mechanism: D-Bus. This is how you set progress values, for example. User choices are returned via exit codes or stdout.

Finally, there is Zenity. It's a Gnome project, and you should take a look at it if you need GTK+ dialogs. The command-line syntax is again different and pipes are used for IPC as in plain dialog. Among the tools we discussed, Zenity is the only one that can provide a notification bar icon. On a similar note, none of these programs require you to specify dialog geometry, like dialog does. Layout managers in GTK+ and Qt do a great job here.

The Author

Valentine Sinitsyn works in a cloud infrastructure team and teaches students completely unrelated subjects. He also has a KDE Developer account he's never really used.

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Zenity and KDialog

    Zenity and KDialog let you integrate your scripts with the native KDE or Gnome environment.

  • Ruby and Glade

    Application development shouldn’t be a chore. We’ll show you how to simplify the development process with the Ruby programming language and the Glade interface design utility.

  • Perl: PerlPanel

    One panel has a neat collection of applets and another has spectacular looks – but a combination of the two is rare. Now help draws nigh for the desktop: PerlPanel is extensible with do-it-yourself widgets.

  • SuperKaramba Workshop

    If you can’t find the SuperKaramba theme you’re looking for, you can always build your own.

  • Command Line: KDE Kiosk

    KDE Kiosk lets administrators control user environments, including settings, themes, and access to the command shell and designated peripherals.

comments powered by Disqus

Direct Download

Read full article as PDF:

News