加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

当我使用非零参数显式调用exit时,为什么我的Perl脚本返回零返回

发布时间:2020-12-15 21:48:52 所属栏目:大数据 来源:网络整理
导读:我有一个调用另一个脚本的Perl脚本. Perl脚本应该传播脚本的返回代码,但似乎返回零到其调用者( Java应用程序)desipte显式调用退出$scriptReturnCode. 代码和输出如下(我意识到 =可能/应该是!=但这就是我所拥有的): print "INFO: Calling ${scriptDirectory
我有一个调用另一个脚本的Perl脚本. Perl脚本应该传播脚本的返回代码,但似乎返回零到其调用者( Java应用程序)desipte显式调用退出$scriptReturnCode.

代码和输出如下(我意识到< =>可能/应该是!=但这就是我所拥有的):

print "INFO: Calling ${scriptDirectory}/${script} ${args}"
$scriptReturnCode = system("${scriptDirectory}/${script} ${args}");

if ( $scriptReturnCode <=> 0 ) {
        print "ERROR: The script returned $scriptReturnCoden";
        exit $scriptReturnCode;
} else {
        print "INFO: The script returned $scriptReturnCode.n";
        exit 0;
}

我从Java获得的输出是:

20/04/2010 14:40:01 - INFO: Calling /path/to/script/script.ksh arg1 arg2 
20/04/2010 14:40:01 - Could not find installer files <= this is from the script.ksh    
20/04/2010 14:40:01 - ERROR: The script returned 256
20/04/2010 14:40:01 - Command Finished. Exit Code: 0 <= this is the Java app.

解决方法

您需要将系统()调用的返回码移位8位.

例如. $exit_value = $? >> 8; #在你的脚本中$?是$scriptReturnCode

从http://perldoc.perl.org/perlfaq8.html开始:

system() runs a command and returns exit status information (as a 16 bit value: the low 7 bits are the signal the process died from,if any,and the high 8 bits are the actual exit value

更加扩展的代码检查coredump也可能如下所示:

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

更新:根据ysth的优秀提醒,退出代码被截断为8(低)位,因此返回256而不是预期的1最终为0.同样,返回257最终为1.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读