Question

I was porting one of my existing scripts into perl and ran into some difficulty with executing system commands (specifically lftp). Here is how I did it in bash:

BASH:

lftp -u $ftpusername,$ftppass -e open $hostname << EOF
      set ftp:ssl-allow no
      set mirror:use-pget-n 5
      glob -a rm -r "${remote_dir}/file.php"
EOF

So I'm wondering how I can execute this exact same command in Perl. Here's what I've tried with no avail:

Perl:

system("lftp -u $ftpuser,$pass -e open $hostname
            set ftp:ssl-allow no
            set mirror:use-pget-n 5
            glob -a rm -r 'remote_dir/db-import.php'"
      );

Any help would be appreciated, thank you!

Was it helpful?

Solution

perl: feeding stdin of executed command

You may use open to open pipe to executed program (lftp) input.

open( my $LFTP,'|-', "lftp -u $ftpuser,$pass -e open $hostname" ) 
  or die "Cannot open lftp: $!";
print $LFTP <<"END";
set ftp:ssl-allow no
set mirror:use-pget-n 5
glob -a rm -r 'remote_dir/db-import.php'
END
close($LFTP) or die; # die unless lftp exit code is 0

Alternative method: Using module like IPC::Run allows to check executed command replies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top