use IPC::Open3; ($command = $0) =~ s#.*/##; # Use # as delimiter (simpler expr) ($bindir = $0) =~ s#/$command##; # $bindir = "./"; $rcp = "/usr/bin/scp"; $rsh = "/usr/bin/ssh -l cs "; open(my $in, "<", "lista.txt"); @fetchfiles = <$in>; $tpcno = 89; $tpc = "tpc089"; $fsize= $#fetchfiles+1; print "About to fetch $fsize.\n"; @rcpout = (); if ($childspid = open3(TWTR,TRDR,TERR, "$rsh cs\@$tpc tar cf - --files-from=- -C video | tar tvf - | wc -l" )) { for (@fetchfiles){ @dest = split(/\//); $_ = $dest[$#dest]; print(TWTR "$_\n"); print $i++; } close(TWTR); @rcpout = ; close(TRDR); @rcperr = ; close(TERR); if ($rcperr || ($rcpout[0] != @fetchfiles)) { chop($rcperr); if($rcperr){ print($tpcno, ' - TPC Download error: ',"@rcperr"); }else { $rc = $rcpout[0]; print($tpcno, ' - TPC Download error: ',"read $rc files instead of $fsize"); } return; } else { print "Successfully fetched $fsize files.\n"; } wait; }else { print "failed open3: $childspid \n"; }