Return script shell status value when calling from ruby?

I expected these values ​​to match. They did not match when the script shell exited due to some error condition (and thus returned a nonzero value). Shell $? returned 1, ruby ​​$? 256 returned.

>> %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 
+5
source share
2 answers

There $?is an instance in Ruby Process::Status. Printing is $?equivalent to a call $?.to_s, which is equivalent $?.to_i.to_s(from the documentation).

to_idoes not match with exitstatus.

From the documentation:

Posix 16- . (, , ) , , ( ).

$?.to_i 16- , , , , exitstatus:

$?.exitstatus
+16

. http://pubs.opengroup.org/onlinepubs/9699919799/functions/exit.html:

0, EXIT_SUCCESS, EXIT_FAILURE, [CX] , 8 ( 0377).

unix 8 . 256 , , undefined. , Mac OS 10.7.3 Ruby 1.9.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>

,

$ sh -c 'exit 256'; echo $?
0 
$ sh -c 'exit 257'; echo $?
1

shell- script ( ), < 256.

0

All Articles