我正在尝试在perl中捕获python exit coode表单。
我的python代码py_test.py:
import sys
sys.exit(sys.argv[1])
我的perl代码test.pl:
#!/usr/bin/perl -w
$res = system("python py_test.py ".$ARGV[0]);
print("\$res ==== $res \n >>>". $? ."\n");
我跑了一下:
perl test.pl 4
结果是:
4
$res ==== 256
>>>256
我在32位RHEL 5.3上使用python 2.4.3和perl 5.8.8。
有人能帮我理解这里发生了什么吗,我似乎不能理解这种行为,我做错了什么吗?(我看了这段代码几个小时,我从来没有遇到过系统这么奇怪的事情)
发布于 2018-07-10 13:40:56
我找到我的问题了。我输入了sys.exit argv1,但在开始时python将其视为字符串。在我把它改成sys.exit(int(argv1))之后,一切都运行得很好。
这也解释了为什么它打印了"4"...
发布于 2018-07-10 07:09:27
来自"perldoc perlvar“
$? The status returned by the last pipe close, backtick (``) command,
successful call to "wait()" or "waitpid()", or from the "system()"
operator. This is just the 16-bit status word returned by the
traditional Unix "wait()" system call (or else is made up to look
like it). Thus, the exit value of the subprocess is really ("$? >>
8"), and "$? & 127" gives which signal, if any, the process died
from, and "$? & 128" reports whether there was a core dump.
听起来您的python脚本可能无法成功退出4。
test.py
#!/usr/bin/env python3
import sys
sys.exit(4)
test.pl
#!/usr/bin/env perl
use warnings;
use strict;
system("./test.py");
print "\$? = $?\n"
die("Killed by signal ".( $? & 0x7F ) if $? & 0x7F;
die("Exited with error ".( $? >> 8 ) if $? >> 8;
print "Success\n";
结果:
~/test$ ./test.pl
$? = 1024
Exited with error 4
https://stackoverflow.com/questions/51253378
复制相似问题