Zenity and rsync

Published on 24 Dec 2008 Zenity awk rsync

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 and rsync

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.

David Verhasselt

I’m a consultant & entrepreneur. I build webapps and optimize Minimal Viable Products for clients all over the world. If you'd like to chat, hit me up on david@crowdway.com.

Like this? Sign up to get regular updates
David
Verhasselt