做
system('downloadImage', '-url', $url, '>', $log)
在Perl
是一樣的,在外殼做
'downloadImage' '-url' "$url" '>' "$log"
。這執行downloadImage
與四個參數,其中之一是>
。您嘗試執行的shell命令如下:
'downloadImage' '-url' "$url" > "$log"
如果要執行該shell命令,首先需要一個shell。以下是你應該如何實現這一點:
use String::ShellQuote qw(shell_quote);
my @cmd = ('downloadImage', '-url', $url);
my $shell_cmd = shell_quote(@cmd) . ' >' . shell_quote($log);
system($shell_cmd); # Short for system('/bin/sh', '-c', $shell_cmd);
不過,也有很多缺點,以使用shell。以下是一些備選:
use IPC::Open3 qw(open3);
my @cmd = ('downloadImage', '-url', $url);
{
open(local *CHILD_STDIN, '<', '/dev/null')
or die("Can't open \"/dev/null\": $!\n");
open(local *CHILD_STDOUT, '>', $log)
or die("Can't create \"$log\": $!\n");
my $pid = open3('<&CHILD_STDIN', '>&CHILD_STDOUT', '>&STDERR', @cmd);
waitpid($pid, 0);
}
或
use IPC::Run3 qw(run3);
my @cmd = ('downloadImage', '-url', $url);
run3(\@cmd, \undef, $log);
或
use IPC::Run qw(run);
my @cmd = ('downloadImage', '-url', $url);
run(\@cmd, '<', '/dev/null', '>', $log);
你是如何執行'@ command'? – Schwern