Search code examples
perlbufferrsyncwhiptail

How to use Perl with the Whiptail gauge?


I can track the rsync progress via Whiptail, using Awk to parse the rsync output, however, I'm puzzled by why the Perl counterpart doesn't work (the Whiptail gauge stays stuck at 0).

This is the working Awk commandline:

rsync --info=progress2 --no-inc-recursive --human-readable <source> <destination> |
  stdbuf -o0 awk -v RS='\r' '$2 ~ /%$/ { print substr($2, 0, length($2) - 1) }' |
  whiptail --gauge Syncing 20 80 0

This is the Perl (I assume) equivalent:

rsync --info=progress2 --no-inc-recursive --human-readable <source> <destination> |
  stdbuf -o0 perl -lne 'BEGIN { $/ = "\r" } print /(\d+)%/' |
  whiptail --gauge Syncing 20 80 0

If I remove the Whiptail command from the Perl version, the percentage numbers are printed as expected.

How do I need to modify the Perl version?


Solution

  • You may be suffering from buffering. Try setting autoflush on STDOUT.

    BEGIN { $/ = "\r"; $|++ }
    

    or if Perl is at least version 5.14, or otherwise with adding the -MIO::Handle switch, you can be more explicit:

    BEGIN { $/ = "\r"; *STDOUT->autoflush }