[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

shell exit code

Yuguri Azuma

4/12/2009 8:49:00 AM

Hello Group,

Is there any way to retrieve an exit code of shell commands when called
from ruby?

ruby code :

exec("ls") if fork.nil?
Process.wait

shell :

# ls
# echo $? <-- get this value in ruby
--
Posted via http://www.ruby-....

2 Answers

Alex Eiras

4/12/2009 11:09:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

>
> Hello Group,
>
> Is there any way to retrieve an exit code of shell commands when called
> from ruby?
>
> ruby code :
>
> exec("ls") if fork.nil?
> Process.wait
>
> shell :
>
> # ls
> # echo $? <-- get this value in ruby
> --
>

As of Ruby 1.9, you indeed have the $? global variable, which references the
exit status of the last child process to terminate. In your example, exec
will replace the subprocess by running the given command, thus $? will
contain its exit status.

irb(main):001:0> exec('ls') if fork.nil?; Process.wait; puts $?
tmp
pid 5552 exit 0

irb(main):002:0> exec('cat /etc/sudoers') if fork.nil?; Process.wait; puts
$?
cat: /etc/sudoers: Permission denied
pid 5556 exit 1

Hope it helps
Cheers

Alex

Yuguri Azuma

4/12/2009 11:33:00 AM

0

Hello Alex,

Thank you for your advise. This really helped!

Regards,
Yuguri

Alex Eiras wrote:
>>
>> shell :
>>
>> # ls
>> # echo $? <-- get this value in ruby
>> --
>>
>
> As of Ruby 1.9, you indeed have the $? global variable, which references
> the
> exit status of the last child process to terminate. In your example,
> exec
> will replace the subprocess by running the given command, thus $? will
> contain its exit status.
>
> irb(main):001:0> exec('ls') if fork.nil?; Process.wait; puts $?
> tmp
> pid 5552 exit 0
>
> irb(main):002:0> exec('cat /etc/sudoers') if fork.nil?; Process.wait;
> puts
> $?
> cat: /etc/sudoers: Permission denied
> pid 5556 exit 1
>
> Hope it helps
> Cheers
>
> Alex

--
Posted via http://www.ruby-....