LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-08-2009, 04:10 PM   #1
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
[Ruby on Rails] backticks don't work under rails.


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.
 
Old 09-08-2009, 04:19 PM   #2
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Original Poster
Rep: Reputation: 47
Bah. I'm an idiot.

There's an error sitting under script/server:

Code:
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_controller/base.rb:385: command not found: cmd
changed the call to `cmd`

Code:
        output = `cmd`
to

Code:
        output = `#{cmd}`
Worked like a charm.

Do I get to thank myself for the solution? ;-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any issues installing Ruby Gems and Ruby on Rails in Slackware? Lufbery Slackware 8 02-09-2011 07:22 PM
How do I get Ruby on Rails Application to work? RavenLX Linux - Server 0 06-15-2009 04:06 PM
LXer: Real world Rails: Caching in Rails LXer Syndicated Linux News 0 05-19-2007 09:16 PM
Getting started with Ruby on Rails Ephracis Linux - Server 1 09-24-2006 03:40 PM
LXer: Ruby off the Rails LXer Syndicated Linux News 0 12-24-2005 04:31 PM


All times are GMT -5. The time now is 02:57 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration