Zenity is a neat little tool to create simple GUI’s for your shellscripts. One of its most useful features is the progress dialog, which allows one to show the progress of a command using the all-familiar GTK progress bar.

Zenity uses pipes to send commands to the dialogs. Any number sent to the Zenity instance while in progress mode will make the progress bar move to that number as the percentage completed. Any text that starts with # is set as the label above the progress bar.
Here’s an example shamelessly stolen and abbreviated from the manual:
#!/bin/sh
(
echo "10" ; sleep 1
echo "# Updating mail logs" ; sleep 1
echo "20" ; sleep 1
echo "# Resetting cron jobs" ; sleep 1
echo "75" ; sleep 1
echo "# Rebooting system" ; sleep 1
echo "100" ; sleep 1
) |
zenity --progress \
--title="Update System Logs" \
--text="Scanning mail logs..." \
--percentage=0
To shape the output of a real application into data fit for Zenity mostly requires some creative awking. I couldn’t find an example to parse rsync output, so I made this awk-script to show the progress of an rsync operation:
{
if (index($0, "to-check=") > 0)
{
split($0, pieces, "to-check=")
split(pieces[2], term, ")");
split(term[1], division, "/");
print (1-(division[1]/division[2]))*100"%"
}
else
{
print "#"$0;
}
fflush();
}
Save it as rsync.awk and use it like this:
$ rsync -av --progress /media/disk/ ~/backup/usbstick/ |
awk -f rsync.awk |
zenity --progress --title "Backing up USB-Stick" \
--text="Scanning..." --percentage=0 --auto-kill
Mind how we use the parameter progress to tell how far we’ve progressed. This results in the dialog shown above.
Thanks to Florian Purucker for the awk-script update which fixed a bug causing the progress to be calculated incorrectly, and to Matt Raines for the tip on –auto-kill.
Nice. Op den duur wordt Linux nog eens gebruiksvriendelijk zeg!
Great idea with awk and zenity. You just saved me from a headache with other programming languages.
As I don't understand your awk script very well, would you be able to come up with the same for dialog (cli version)
man dialog, see –gauge option
Thanks for this post! I was thinking about how to get a total progress for rsync for quite a while, and this finally encouraged me to just do it. Until rsync includes some total progress functionality, this is quite nice.
I wanted to also get the original rsync output in the Terminal, so I made a slightly different version, and used Perl rather than awk, because I know it better. In case someone finds it useful, here is the version I'm using.
<pre>
#!/usr/bin/perl
## Parse rsync –verbose –progress output for zenity
$|++;
while (<>) {
warn $_;
if ( m|^s+.*to-check=(d+)/(d+)| ) {
printf "%.0f
", 100 – (100 * $1 / $2);
}
else {
print "#$_";
}
}
</pre>