I'm trying to use backticks to run a perl script from within my rails code. I know that this isn't good style, but I'm trying to mock something up in Rails that I already have written in Perl.
I simplified my code to illustrate the error (calling 'ls /tmp' instead of the perl script).
The controller is in ./app/controllers/missed_controller.rb (named missed_controller for hysterical raisins)
Code:
class MissedController < ApplicationController
def get_missed
cmd = "/bin/ls /tmp"
output = `cmd`
if( output == nil )
@missing = "missing is NIL"
else
@missing = "The output of '#{cmd}' is [#{output}]"
end
end
end
here's the view (from ./app/views/missed/get_missed.html.erb)
Code:
<html>
<head> <title> Testing backticks in Rails </title> </head>
<body>
<h1> <%= @missing %> </h1>
</body>
</html>
I fire up ./script/server and point my browser to
http://localhost:3000/missed/get_missed
Viewing html code, I see:
Code:
<html>
<head> <title> Testing backticks in Rails </title> </head>
<body>
<h1> The output of '/bin/ls /tmp' is [] </h1>
</body>
</html>
I'm expecting the output of '/bin/ls /tmp' to show up in between the brackets, but that's not happening.
If I run the same command in a straight ruby script, it works:
Code:
#! /usr/bin/ruby
puts `ls /tmp`
does in fact give me a directory listing of /tmp.
Here's the system that I'm running:
Debian Etch
> ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
> rails -v
Rails 2.3.4
I grabbed ruby 1.8.7 from Etch backports, compiled rubygems from source, then installed rails 2.3.4 as using gem install.
As far as I can tell, there's something about rails that isn't allowing me to do this. I'm guessing that there's a security setting somewhere that I need to disable in order to use backticks or popen from within Rails, but I don't know what to turn off.