shell exit code

Y

Yuguri Azuma

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
 
A

Alex Eiras

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,498
Latest member
yelene6679

Latest Threads

Top