0

2 个有问题的示例:以下语句语法有什么问题(perl 新手):

$mailCmd = sprintf("echo $message | /usr/ucb/mail -s 'X Detected an Invalid Thing' %s", $people_list);

当我做system($mailCmd)or`$mailCmd`时,它会导致:

sh: syntax error at line 2: `|' unexpected

另一个:

$message = "Invalid STUFF setup for ID $r.  Please correct this ASAP.\n" .
            "Number of thingies  = $uno \n"   .
            "Another thingy      = $id  \n" ;

这会产生:

sh: Number: not found
sh: Another: not found

提前致谢

4

1 回答 1

3

第一个问题的直接原因是您正在执行以下命令,因为内容$message以换行符结尾。

echo ...
| /usr/usb/mail ...

这两个问题都是由于 shell 命令构造不当造成的。固定的:

use String::ShellQuote qw( shell_quote );
my $echo_cmd = shell_quote('echo', $message);
my $mail_cmd = shell_quote('/usr/ucb/mail',
   '-s' => 'X Detected an Invalid Thing',
   $people_list,
);
system("$echo_cmd | $mail_cmd");

或者完全避免echo和外壳:

use IPC::Run3 qw( run3 );
my @cmd = ('/usr/ucb/mail',
   '-s' => 'X Detected an Invalid Thing',
   $people_list,
);
run3 \@cmd, \$message;
于 2013-06-07T05:06:38.970 回答