3

I started working on a perl script and I'm calling external rsync application to do a backup. I want to capture the output of the rsync action and I'm using this format:

print "Starting backup. Please wait...\n";
my @output = `rsync -avut /home /media/drive/`;

At this point, the script is running as is supposed to, but the action of the rsync is captured into my array and I can't see the progress. Is there a way to capture the output as above, but also show it in my console?

Marian Boricean
  • 110
  • 1
  • 11

3 Answers3

4

The backticks capture STDOUT and places it in your array.

If you want to see STDOUT as well as capture it in a variable, you could use open().

I think something like this might work?

my $output;
print "Starting backup. Please wait...\n";
open(RSYNC, "-|", "rsync -avut /home /media/drive") 
    or die "Can't exec rsync : $!";
while(<RSYNC>)
{
  print $_;
  $output .= $_;
}
close(RSYNC);
Anthony
  • 3,492
  • 1
  • 14
  • 9
  • Great Anthony. This works like a charm. I did read about "open" option in perldoc, but I didn't understand it at the time how it worked. – Marian Boricean Aug 22 '13 at 13:45
0

something like this might do the trick

use strict;

my $fh;

open $fh, "find /|"; #don't forget to check it opens!!

while (<$fh>){
   chomp;
   print "here--$_\n";
}
KeepCalmAndCarryOn
  • 8,817
  • 2
  • 32
  • 47
0

You can take a look at this related question. The accepted answer suggests using Capture::Tiny.

Community
  • 1
  • 1
Unos
  • 1,293
  • 2
  • 14
  • 35