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 Mornini On Dec 13, 2005, at 8:42 PM, Jonathan Conway wrote:> 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 > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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 Tom Mornini wrote:> Your PATH environment variable or a fully qualified path to date? > > -- -- Tom Mornini > > On Dec 13, 2005, at 8:42 PM, Jonathan Conway wrote: > >> 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 >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails
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