Note that there are some explanatory texts on larger screens.

plurals
  1. POpassing arguaments to a system call in Rails, but not plain ruby program
    text
    copied!<p>Ubuntu 12.04</p> <p>Sinatra 1.3.3</p> <p>Why does passing an argument to a ruby system call (%x[] or ``) give me a 'not found' error in my sinatra app? The same code works fine in a normal ruby script running from the same directory.</p> <p>I have a file test.rb like this</p> <pre><code>output = %x["ls"] p output </code></pre> <p>When I run it with "ruby test.rb" I get the contents of the current directory in the console, as expected.</p> <p>If I modify the program to give an argument to the system call like so:</p> <pre><code>output = %x["ls sub_dir/"] p output </code></pre> <p>I get the contents of sub_dir, which sits in the current directory, as expected.</p> <p>So far so good.</p> <p>Now if I make a Sintra app with a post method:</p> <pre><code>require 'rubygems' require 'bundler/setup' require 'sinatra' post "/" do output = x["ls"] return output end </code></pre> <p>The response to a Post call to "/" returns the contents of the current directory, which includes 'sub_dir', as expected.</p> <p>If I try to add the argument to the system call to the sinatra app like so:</p> <pre><code>require 'rubygems' require 'bundler/setup' require 'sinatra' post "/" do output = x["ls sub_dir/"] return output end </code></pre> <p>the response is nil and there is an error in the console:</p> <blockquote> <p>sh: 1: ls sub_dir/: not found</p> </blockquote> <p>Why does adding a parameter to a system call in my sinatra app cause it to crash, when the same code called from a plain ruby script, run from the same location works perfectly.</p> <p>By the way, the 'ls' example shown here is not the command I really need to run, so please don't explain a different way to get this information. I have an executable file that takes a file name as a parameter that I need to run, which behaves exactly the same way.</p> <p>Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload