Check comand status in ruby

  • Thread starter Arthur Korneyew
  • Start date
A

Arthur Korneyew

Hello,

How can I check comand status in Ruby ?
In perl I'm usually using something like that:

$cmd="mv $srcdir/$file $dstdir/$dstfile > /dev/null 2>&1";
$result=system($cmd);
$status=$result >> 8;
if ($status eq '0') {
&Logit("Ok");
}
else {
&Logit("Not Ok");
&Logit("$!");
}

I have tried this code in Ruby but it does not working
as I wish:

IO.popen("/usr/bin/cd /tmp") { |process|
result = process.gets
if (result == nil)
logToScreen "Ok"
else
logToScreen "Not Ok"
end
}

Many thanx !
 
T

ts

A> How can I check comand status in Ruby ?

You have the class Process::Status

uln% ri Process::Status
------------------------------------------------- Class: Process::Status
+Process::Status+ encapsulates the information on the status of a
running or terminated system process. The built-in variable +$?+ is
either +nil+ or a +Process::Status+ object.

fork { exit 99 } #=> 26557
Process.wait #=> 26557
$?.class #=> Process::Status
$?.to_i #=> 25344
$? >> 8 #=> 99
$?.stopped? #=> false
$?.exited? #=> true
$?.exitstatus #=> 99

Posix systems record information on processes using a 16-bit
integer. The lower bits record the process status (stopped, exited,
signaled) and the upper bits possibly contain additional
information (for example the program's return code in the case of
exited processes). Pre Ruby 1.8, these bits were exposed directly
to the Ruby program. Ruby now encapsulates these in a
+Process::Status+ object. To maximize compatibility, however, these
objects retain a bit-oriented interface. In the descriptions that
follow, when we talk about the integer value of _stat_, we're
referring to this 16 bit value.

------------------------------------------------------------------------


Instance methods:
-----------------
&, ==, >>, coredump?, exited?, exitstatus, inspect, pid, signaled?,
stopped?, stopsig, success?, termsig, to_i, to_int, to_s
uln%




Guy Decoux
 

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,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top