Forum: Ruby on Rails executing an external application within rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jonathan Conway (Guest)
on 2005-12-14 03:46
(Received via mailing list)
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
Tom M. (Guest)
on 2005-12-14 05:55
(Received via mailing list)
Your PATH environment variable or a fully qualified path to date?

-- -- Tom M.
Jonathan Conway (Guest)
on 2005-12-14 14:05
(Received via mailing list)
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
zdennis (Guest)
on 2005-12-14 16:48
(Received via mailing list)
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
This topic is locked and can not be replied to.