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:
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:
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!
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.