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
-
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
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.