我希望这些值匹配。当shell脚本由于某种错误条件而退出时,它们不匹配(因此返回一个非零值)。Shell $?返回% 1,拼音$?返回256。
>> %x[ ls kkr]
ls: kkr: No such file or directory
=> ""
>> puts $?
256
=> nil
>> exit
Hadoop:~ Madcap$ ls kkr
ls: kkr: No such file or directory
Hadoop:~ Madcap$ echo $?
1 发布于 2012-04-30 02:08:37
在Ruby语言中,$?是一个Process::Status实例。打印$?相当于调用$?.to_s,相当于文档中的$?.to_i.to_s。
to_i与exitstatus不同。
从文档中:
Posix系统使用16位整数记录有关进程的信息。较低的位记录进程状态(已停止、已退出、已发出信号),较高的位可能包含附加信息(例如,在进程已退出的情况下程序的返回码)。
$?.to_i将显示整个16位整数,但是您需要的只是退出代码,因此需要调用exitstatus
$?.exitstatus发布于 2012-04-30 02:04:08
请参阅http://pubs.opengroup.org/onlinepubs/9699919799/functions/exit.html
status的值可以是0、EXIT_SUCCESS、EXIT_FAILURE、CX或任何其他值,尽管只有最低有效的8位(即status & 0377)可用于等待的父进程。
unix退出状态只有8位。256溢出,所以我猜这种情况下的行为是未定义的。例如,在带有Ruby 1.9.3的Mac OS 10.7.3上会发生这种情况:
irb(main):008:0> `sh -c 'exit 0'`; $?
=> #<Process::Status: pid 64430 exit 0>
irb(main):009:0> `sh -c 'exit 1'`; $?
=> #<Process::Status: pid 64431 exit 1>
irb(main):010:0> `sh -c 'exit 2'`; $?
=> #<Process::Status: pid 64432 exit 2>
irb(main):011:0> `sh -c 'exit 255'`; $?
=> #<Process::Status: pid 64433 exit 255>
irb(main):012:0> `sh -c 'exit 256'`; $?
=> #<Process::Status: pid 64434 exit 0>这与我的shell所显示的内容一致
$ sh -c 'exit 256'; echo $?
0
$ sh -c 'exit 257'; echo $?
1我建议您修复shell脚本(如果可能),使其仅返回值< 256。
https://stackoverflow.com/questions/10374418
复制相似问题