在 Perl 中读取响应

Read responses in Perl

在我下面的代码中,如何从 su 命令获取结果? @output 仅包含 myCmd 的结果,但不包含 su 命令的结果。谢谢!

 $cmd = "su test -c 'myCmd'";
 $pid = open2(*README,*WRITEME, $cmd);
 $pw='abc';
 print WRITEME "$pw\n";
 @output = <README>;
 waitpid($pid, 0);
 my $child_exit_status = $?;
 close(WRITEME);
 close (README);
 foreach $lf (@output) {
   print $lf;
 print $child_exit_status;

错误输出将转到 stderr。使用IPC::open3捕获它。

根据@ikegami 的评论:使用 open3 读取 STDOUT 和 STDERR 是极其复杂的。继续使用 open2 但添加 2>&1,或使用 IPC::Run