让 Perl 返回正确的退出代码 [英] Getting Perl to return the correct exit code
问题描述
我在 Ubuntu 11.04 上使用 Perl 5.10.1.我希望 Perl 执行一个 shell 脚本并使用 shell 脚本退出的相同代码退出.但这对我不起作用......
I'm using Perl 5.10.1 on Ubuntu 11.04. I want Perl to execute a shell script and exit with the same code the shell script exits. But this isn't working for me ...
system($runCmd) or die("Failed to run \"$runCmd\": $!");
我已确认单独运行$runCmd"会返回退出代码 255,但不会调用die"子句.如何以正确的代码退出或至少因非成功代码而失败?
I have confirmed that running the "$runCmd" by itself returns an exit code of 255, but the "die" clause isn't getting invoked. How do I exit with the correct code or at least fail for non-success codes?
另一个次要要求是我希望将 $runCmd 的输出打印到屏幕上.
Another minor requirement is that I want the output from $runCmd to be printed to the screen.
推荐答案
As perldoc -f die
说, die
没有给出特定的退出代码(只有一个非零的退出代码).为了得到你想要的东西,你需要这样的东西:
As perldoc -f die
says, die
doesn't give a specific exit code (only a nonzero exit code). To get what you want, you'll need something like:
my $exit_code=system($runCmd);
if($exit_code!=0)
{
print "Command $runCmd failed with an exit code of $exit_code.\n";
exit($exit_code >> 8);
}
else
{
print "Command $runCmd successful!\n";
}
这篇关于让 Perl 返回正确的退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!