Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl-$?在Windows 2008 64位上为-1返回码显示0_Perl_System - Fatal编程技术网

Perl-$?在Windows 2008 64位上为-1返回码显示0

Perl-$?在Windows 2008 64位上为-1返回码显示0,perl,system,Perl,System,使用草莓Perl 5.12.3 手动运行: E:\informatica\tools>infacmd isp ping -sn tt -re 0 [ICMD_10033] Command [ping] failed with error [[INFACMD_10053] Service [tt] Domain [dmt3-9-dom-poc] has failed to ping back.]. E:\informatica\tools>echo %ERRORLEVEL% -1

使用草莓Perl 5.12.3

手动运行:

E:\informatica\tools>infacmd isp ping -sn tt -re 0
[ICMD_10033] Command [ping] failed with error [[INFACMD_10053] Service [tt] Domain [dmt3-9-dom-poc] has failed to ping back.].

E:\informatica\tools>echo %ERRORLEVEL%
-1
当我通过Perl的“系统”运行相同的命令时,$?显示0。Perl代码:

use strict;
use warnings;
my $cmd = system("infacmd isp ping -sn tt -re 0");
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;    
}
输出:

[ICMD_10033] Command [ping] failed with error [[INFACMD_10053] Service [tt] Domain     [dmt3-9-dom-poc] has failed to ping back.].
child exited with value 0

Windows 2003 32位和ActiveState Perl 5.8.8上也显示了正确的结果。

我认为您应该将$移位?(即除以256)在进行分支之前

$res = $? >>8;
if ($res == -1)
....

编辑:我刚刚阅读了Perl5中的示例(章节:处理错误和信号),说明在windows上不能依赖$?表示管道和系统的退出状态。这很可能是你的案子

然后,建议捕获命令输出并对其进行解析,以找出错误代码

 my $output = `mycmd 2>&1`;
 if ($output =~ /.....

我认为您的手动测试还不够,因为“errorlevel”变量可能会被隐藏,或者使用shell可能会混淆问题,因此您的“infacmd”可能不会以您认为正确的退出代码退出

您的perl脚本正在通过shell调用此子流程。如果直接调用它,行为是否会改变?(这通常是良好的实践…)

i、 e.如果将
系统
行更改为:

   my $cmd = system('infacmd', 'isp', 'ping', '-sn', 'tt', '-re', '0');

。。。行为是否受到影响?

与模拟unix进程状态结构相比,使用
${^CHILD\u ERROR\u NATIVE}
可能会更幸运。

这就剩下了。如果退出代码为零,则退出代码为零。

虽然您的响应符合perlfunc规则,但IIRC,
0/256==0
谢谢,如果不起作用,请将$?>>8放在前面。作为最后手段,我将不得不按照您的建议解析输出,但我仍在寻找合适的解决方案。我想知道这是怎么可能的,因为
ExitThread
TerminateThread
采用无符号整数。与文档相反,使用列表表单并不会阻止shell在Windows上使用。也就是说,如果
infacmd
可以在没有外壳的情况下启动,那么在这种情况下就不会使用它。这是非常有用的信息。你知道这是ActivePerl特有的吗?还是草莓perl和msys perl也是如此?@Friend Pal,我想它会影响AP和草莓,但不会影响cygwin。请尝试
系统(“dir”和“*”)
查找。根据文档,它应该会失败。@Friend Pal,请注意
系统({'dir'}'dir','*')
确实会失败。@Friend Pal,我尝试了你的建议,但没有成功。同样的结果。目前,我还没有在Win2008上的AP上进行测试,但Win2003上的AP按预期返回255。它也返回零。我很困惑。