return code from Open3.popen?

A

Andres Salomon

Hi,

Is there a (non-hackish) way to get the error code from Open3.popen()?
The only way I seem to be able to do it currently is by appending "echo
$?" to the command, and parsing it out of stdout. $?.exitstatus contains
some random value.
 
D

Daniel Berger

Andres said:
Hi,

Is there a (non-hackish) way to get the error code from Open3.popen()?
The only way I seem to be able to do it currently is by appending "echo
$?" to the command, and parsing it out of stdout. $?.exitstatus contains
some random value.

The normal way of getting an error out of Open3.popen3 is to check the
STDERR handle:

fin, fout, ferr = Open3.popen3("ls")
error = ferr.gets
if error
puts "Error: " + error.to_s # There was an error
end
....

Regards,

Dan
 
M

Martin Kaletsch

Daniel Berger wrote:

The normal way of getting an error out of Open3.popen3 is to check the
STDERR handle:

fin, fout, ferr = Open3.popen3("ls")
error = ferr.gets
if error
puts "Error: " + error.to_s # There was an error
end

I know of at least one programm using stderr for output even when execution
was successfull!
 
D

dilinger

Yes, there are certainly plenty out there.

Really, there needs to be a consistent interface for this sort of
thing; there are plenty of functions out there that execute commands
(IO.popen, Kernel.system, Open3.popen3, etc) that all do slightly
different things wrt $?, depending upon their implementation. That
should be hidden from the user; it should also be possible to define
your own method to run commands in ruby, and override $? (it is
currently read-only from within ruby). Personally, I'd rather see an
exception thrown if the command fails, perhaps w/ an additional
argument to specify that you don't care if the command fails. How that
should happen within IO.popen()'s block is interesting, though. As it
stands, $? is not set until the block exits. So:

IO.popen('nonexistantcommand') { |f|
puts $?.exitstatus
}
puts $?.exitstatus

The first will return the last command run *before* the popen. If the
command exits immediately (ie, because the command doesn't exist),
should an exception be thrown as soon as it is realized that the
command failed? Or, should the block be executed regardless of whether
the command failed or not, and an exception thrown after the block as
completed?
 
D

Daniel Berger

I don't see this as an issue with Ruby, but with the programs that
don't indicate success vs failure properly. There is simply no
consistent way to determine failure with such programs that I know of.

You will have to create special wrappers for whatever it is you're
trying to execute. I had to do this with one of my own packages, where
warnings were sent to STDERR, though the program attempt didn't
actually fail. I literally had to parse warning messages and handle
that separately.

Dan
 

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

Similar Threads

Open3 1
popen, open3 problems 3
open3 and background processes 3
Driving Oracle sqlplus with open3 11
Open3 and ssh 0
[ANN] win32-open3 0.1.0 0
IPC::open3: What goes wrong? 2
Snippet: The leanest Popen wrapper 10

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top