Executing an external application within rails

Hi,
I seem to have come across a strange problem. I need to execute an
external application, grab its return value and do something with it.
Sounds fine doesn’t it? Unfortunetely I’m running into some problems.

Just for example sake, lets say I want to execute the date command on
unix I’d do something like test = %x{date} or test IO.popen(‘date’).read
and I should get a value back assigned to test right? Well it seems it
only works from inside IRB and using the rails console.

When I execute this code from within a method in a Rails application I
get nothing back. Writing the output to the log show nothing either.

Is there something I’m missing?

Cheers

Jon

Your PATH environment variable or a fully qualified path to date?

– -- Tom M.

Thank I’ve got a little bit further. It now works for date using the
full path /bin/date but if say do something like %x{/usr/local/bin/svn}
(this is just a test example), I thought I should at least get the svn
message asking me for the parameters, but instead I get nothing back.
Very strange.

It works without problem from /script/console and irb… just not withing
a rails app.

Any help greatly appreciated

Jon

Jonathan Conway wrote:

Thank I’ve got a little bit further. It now works for date using the
full path /bin/date but if say do something like %x{/usr/local/bin/svn}
(this is just a test example), I thought I should at least get the svn
message asking me for the parameters, but instead I get nothing back.
Very strange.

If you run “svn” with no arguments it will return a message to you by
writing to STDERR. When you
see the returned text in irb or script/console you are not getting a
return value, you are simply
seeing the message printed to STDERR.

%x{ } and both return strings received from STDOUT only

If you want to capture all output you can use Open3.popen which is
available on *nix systems, and
you can also get it on Windows system by downloading the package from
win32-open3 package from
http://rubyforge.org/projects/win32utils/

require ‘open3’
stdin,stdout,stderr = Open3.popen “svn”
stderr.read # => “Type ‘svn help’ for usage.\n”

Zach