gpt4 book ai didi

perl - 如何在 Perl 中读取外部命令的错误输出?

转载 作者:行者123 更新时间:2023-12-03 15:06:42 24 4
gpt4 key购买 nike

作为更大的 Perl 程序的一部分,我正在检查 diff 的输出针对引用文件的文件夹中输入文件的命令,其中空白输出(匹配)是通过结果,而 diff 的任何输出都是失败结果。

问题是,如果目标文件夹的预期文件数量不足,则抛出的异常 diff 不会作为输出出现,从而导致错误通过。

输出示例:

diff: /testfolder/Test-02/test-output.2: No such file or directory

测试 01:通过

测试 02:通过

代码如下:
$command = "(diff call on 2 files)";
my @output = `$command`;
print "Test-02: ";
$toPrint = "PASS";
foreach my $x (@output) {
if ($x =~ /./) {
$toPrint = "FAIL";
}
}

如果 diff 有任何输出,这是一个快速的黑客工作失败。称呼。有没有办法检查 backticks 中调用的命令引发的异常?

最佳答案

答案在perlfaq8: How can I capture STDERR from an external command?

运行外部命令有三种基本方式:

system $cmd;        # using system()
$output = `$cmd`; # using backticks (``)
open (PIPE, "cmd |"); # using open()

使用 system() 时,STDOUT 和 STDERR 将与脚本的 STDOUT 和 STDERR 位于同一位置,除非 system() 命令将它们重定向。反引号和 open() 仅读取命令的 STDOUT。

您还可以使用 IPC::Open3 中的 open3() 函数。 Benjamin Goldberg 提供了一些示例代码:

要捕获程序的 STDOUT,但丢弃其 STDERR:
use IPC::Open3;
use File::Spec;
use Symbol qw(gensym);
open(NULL, ">", File::Spec->devnull);
my $pid = open3(gensym, \*PH, ">&NULL", "cmd");
while( <PH> ) { }
waitpid($pid, 0);

要捕获程序的 STDERR,但丢弃其 STDOUT:
use IPC::Open3;
use File::Spec;
use Symbol qw(gensym);
open(NULL, ">", File::Spec->devnull);
my $pid = open3(gensym, ">&NULL", \*PH, "cmd");
while( <PH> ) { }
waitpid($pid, 0);

要捕获程序的 STDERR,并让其 STDOUT 进入我们自己的 STDERR:
use IPC::Open3;
use Symbol qw(gensym);
my $pid = open3(gensym, ">&STDERR", \*PH, "cmd");
while( <PH> ) { }
waitpid($pid, 0);

要分别读取命令的 STDOUT 和 STDERR,您可以将它们重定向到临时文件,让命令运行,然后读取临时文件:
use IPC::Open3;
use Symbol qw(gensym);
use IO::File;
local *CATCHOUT = IO::File->new_tmpfile;
local *CATCHERR = IO::File->new_tmpfile;
my $pid = open3(gensym, ">&CATCHOUT", ">&CATCHERR", "cmd");
waitpid($pid, 0);
seek $_, 0, 0 for \*CATCHOUT, \*CATCHERR;
while( <CATCHOUT> ) {}
while( <CATCHERR> ) {}

但是没有真正需要两者都是临时文件......以下应该也能正常工作,而不会出现死锁:
use IPC::Open3;
use Symbol qw(gensym);
use IO::File;
local *CATCHERR = IO::File->new_tmpfile;
my $pid = open3(gensym, \*CATCHOUT, ">&CATCHERR", "cmd");
while( <CATCHOUT> ) {}
waitpid($pid, 0);
seek CATCHERR, 0, 0;
while( <CATCHERR> ) {}

它也会更快,因为我们可以立即开始处理程序的标准输出,而不是等待程序完成。

使用其中任何一个,您都可以在调用之前更改文件描述符:
open(STDOUT, ">logfile");
system("ls");

或者您可以使用 Bourne shell 文件描述符重定向:
$output = `$cmd 2>some_file`;
open (PIPE, "cmd 2>some_file |");

您还可以使用文件描述符重定向使 STDERR 成为 STDOUT 的副本:
$output = `$cmd 2>&1`;
open (PIPE, "cmd 2>&1 |");

请注意,您不能简单地在 Perl 程序中打开 STDERR 作为 STDOUT 的副本,并避免调用 shell 进行重定向。这不起作用:
open(STDERR, ">&STDOUT");
$alloutput = `cmd args`; # stderr still escapes

这失败是因为 open() 使 STDERR 转到 open() 时 STDOUT 所在的位置。然后反引号使 STDOUT 转到字符串,但不要更改 STDERR(它仍然转到旧的 STDOUT)。

请注意,您必须在反引号中使用 Bourne shell (sh(1)) 重定向语法,而不是 csh(1)!关于为什么 Perl 的 system() 和反引号和管道打开都使用 Bourne shell 的详细信息位于 http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz 中的“远比您想要知道的更多”集合中的 vs/csh.whynot 文章中。 .一起捕获命令的 STDERR 和 STDOUT:
$output = `cmd 2>&1`;                       # either with backticks
$pid = open(PH, "cmd 2>&1 |"); # or with an open pipe
while (<PH>) { } # plus a read

要捕获命令的 STDOUT 但丢弃其 STDERR:
$output = `cmd 2>/dev/null`;                # either with backticks
$pid = open(PH, "cmd 2>/dev/null |"); # or with an open pipe
while (<PH>) { } # plus a read

要捕获命令的 STDERR 但丢弃其 STDOUT:
$output = `cmd 2>&1 1>/dev/null`;           # either with backticks
$pid = open(PH, "cmd 2>&1 1>/dev/null |"); # or with an open pipe
while (<PH>) { } # plus a read

交换命令的 STDOUT 和 STDERR 以捕获 STDERR 但将其 STDOUT 保留为我们旧的 STDERR:
$output = `cmd 3>&1 1>&2 2>&3 3>&-`;        # either with backticks
$pid = open(PH, "cmd 3>&1 1>&2 2>&3 3>&-|");# or with an open pipe
while (<PH>) { } # plus a read

要分别读取命令的 STDOUT 和 STDERR,最简单的方法是将它们分别重定向到文件,然后在程序完成后从这些文件中读取:
system("program args 1>program.stdout 2>program.stderr");

在所有这些示例中,排序都很重要。这是因为 shell 严格按照从左到右的顺序处理文件描述符重定向。
system("prog args 1>tmpfile 2>&1");
system("prog args 2>&1 1>tmpfile");

第一个命令将标准输出和标准错误发送到临时文件。第二个命令只在那里发送旧标准输出,旧标准错误显示在旧标准输出上。

关于perl - 如何在 Perl 中读取外部命令的错误输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/777543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com