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.
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.
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.
Infos
- builtins(7) man page: http://man7.org/linux/man-pages/man1/bash.1.html
- bash(1) man page: http://man7.org/linux/man-pages/man1/bash.1.html
- dialog(1) man page: https://linux.die.net/man/1/dialog
« Previous 1 2
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs