Perl:从“system”命令捕获正确的返回值

2024-05-19 16:10:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Perl的初学者。我有一个包含多个NMake命令的Windows批处理脚本。此批处理脚本的一个现有问题是,即使NMake命令在执行期间失败,也无法正确设置ERRORLEVEL

因此,在分析日志文件之前,我们永远不知道该命令是否有效。我调查了一下,但找不到解决办法。一、 然后考虑将这个批处理脚本转换为一个Perl脚本,假设捕获错误会更容易,但似乎不是那么容易:)

每当我运行Perl脚本时,“system”命令总是返回0。我查看了许多不同的链接,发现捕获“system”命令的正确返回状态并不是那么简单。尽管如此,我还是尝试了这些建议,但一切都不顺利。:(一)

让我提一下,被调用的NMake命令在执行期间依次调用许多不同的命令。例如,下面提到的命令输出抛出“致命错误”,实际上是Perl脚本(check_dir.pl)的一部分。对Perl脚本的调用是在NMake文件本身中编写的。

如果我直接调用这个Perl文件(check_dir.pl并检查退出值,就会得到正确的结果,即,命令失败并打印出一个非零的退出值(…意外返回的退出值2)。

尝试了Perl的系统功能,但没有帮助。我使用了以下代码:

system ("nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1");

if ( $? == -1 ) {
    print "Command failed to execute: $!\n";
}
elsif ( $? & 127 ) {
    printf "The child died with signal %d, %s a coredump\n",
    ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without';
}
else {
    printf "child exited with value %d\n", $? >> 8;
}

输出:

.....  
.....  
Unable to open dir: R:\TSM_Latest  
Compressing...NMAKE : fatal error U1077: 'if' : return code '0x2'  
Stop.  
child exited with value 0

也尝试过:

use IPC::System::Simple qw(system);  
my $exit_status = system ("nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1");

if ($exit_status != 0) {  
    print "Failure";  
    exit 3;  
} else {  
    print "Success";  
}

最后尝试了以下模块:

use IPC::Run qw( run timeout );  
run "nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1" or die "NMake returned $?";

似乎什么都没用

如果我对system的返回值的解释不正确,请更正我。


Tags: 文件命令脚本dirwithcdsystempack
1条回答
网友
1楼 · 发布于 2024-05-19 16:10:30

你有:

use IPC::System::Simple qw(system);
my $exit_status = system ("nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1");

考虑到你似乎并不关心实际的产出,你可以尝试

my $exit_status = systemx(nmake => 
                              qw(/f _nt.mak pack_cd),
                              "SUB_PLAT=$PLAT",
                              "DR=$plat",
                          );

以确保绕过cmd.exe并查看是否获得有用的信息。

作为参考,exit codes from nmake are listed here

运行以下程序:

use strict; use warnings;

use IPC::System::Simple qw(systemx);
use Try::Tiny;

my $status = 0;

try   { systemx nmake => qw(/f bogus) }
catch { ($status) = ( /exit value ([0-9])/ ) };

print "Failed to execute nmake. Exit status = $status\n";

产生:

NMAKE : fatal error U1052: file 'bogus' not found
Stop.
Failed to execute nmake. Exit status = 2

以下版本:

use strict; use warnings;

my $status = system nmake => qw(/f bogus);

if ($status) {
    if ($? == -1) {
        print "failed to execute: $!\n";
    }
    elsif ($? & 127) {
        printf "child died with signal %d, %s coredump\n",
        ($? & 127), ($? & 128) ? 'with' : 'without';
    }
    else {
        printf "child exited with value %d\n", $? >> 8;
    }
}

产生:

NMAKE : fatal error U1052: file 'bogus' not found
Stop.
child exited with value 2

事实上,即使我用

my $status = system "nmake /f bogus";

我得到了同样正确和预期的输出。

我用的时候也是

my $status = system "nmake /f bogus 2>&1";

这些观察结果引出了以下问题:

  1. 你在用哪个版本的nmake?

  2. /I选项是否有效?即使您没有从命令行设置它,note the following

/I Ignores exit codes from all commands. To set or clear /I for part of a makefile, use !CMDSWITCHES. To ignore exit codes for part of a makefile, use a dash () command modifier or .IGNORE. Overrides /K if both are specified.

所以,我整理了以下文件:

C:\temp> cat test.mak
test.target: bogus.pl; perl bogus.pl
C:\temp> cat bogus.pl
exit 1;

还有,兰:

use strict; use warnings;

my $status = system "nmake /f test.mak 2>&1";

if ($status) {
    if ($? == -1) {
        print "failed to execute: $!\n";
    }
    elsif ($? & 127) {
        printf "child died with signal %d, %s coredump\n",
        ($? & 127), ($? & 128) ? 'with' : 'without';
    }
    else {
        printf "child exited with value %d\n", $? >> 8;
    }
}

它给了我输出:

        perl bogus.pl
NMAKE : fatal error U1077: 'c:\opt\perl\bin\perl.EXE' : return code '0x1'
Stop.
child exited with value 2

最后一行显示nmake的退出状态已正确传播。

结论:

您还有其他问题。

事实上,该组织后来在评论中指出:

The actual command that i am trying to run is: system ("nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1 | C:\\tee2 $TEMP_DIR\\modules-nt_${platlogfile}");

考虑到管道中有tee的参与,因此nmake的退出代码丢失也就不足为奇了。tee能够成功地处理来自nmake的输出,因此它返回success,这是脚本看到的退出代码。

因此,解决方案是自己捕获nmake的输出,可以使用qx(加上适当的错误检查级别),也可以使用IPC::System::Simple中的capture。然后,您可以决定是否要打印输出、保存到文件、将其放入电子邮件等

相关问题 更多 >