参见英文答案 >
Why is Perl’s $? returning the wrong value for the exit code of a forked process? 2个
我有以下perl脚本(test.pl):
我有以下perl脚本(test.pl):
my $exit_code = system('./test.py'); print $exit_code."\n";
试图从python可执行文件(test.py)捕获退出代码:
#!/bin/env python import sys sys.exit(2)
直接运行python可执行文件返回2,这是我的预期:
> ./test.py > echo $? 2
但是,运行perl会返回不同的内容:
> perl test.pl 512
解决方法
孩子可能甚至没有打电话给退出.因此,系统的返回值(又名$?)包含的信息多于退出参数.
if ( $? == -1 ) { die "Can't launch child: $!\n"; } elsif ( $? & 0x7F ) { die "Child killed by signal ".( $? & 0x7F )."\n"; } elsif ( $? >> 8 ) { die "Child exited with error ".( $? >> 8 )."\n"; } else { print "Child executed successfully\n"; }
这是documented.