Hommer Smith
Hommer Smith

Reputation: 27852

Passing urls to exec command in Ruby

I need to call a shell command in Ruby, which receives an argument that is a URL. Here is how I am doing it:

output = `casperjs myscript.js #{url}`

However, if the URL contains weird parameters such as '(' or '|' I get errors from the shell. For example:

When passing 'https://www.theshadestore.com/window-treatments/product/solar-shade?preselected_collections[]=3-percent-basics&preselected_collections[]=5-percent-basics&preselected_collections[]=10-percent-basics'

I get the following error:

sh: 1: preselected_collections[]=10-percent-basics: not found
sh: 1: preselected_collections[]=5-percent-basics: not found

And when I pass: 'http://www.coastal.com/nike-8206-200-brown?rsView=1&ga=F|M|K'

I get:

sh: 1: M: not found
sh: 1: K: not found

How can I avoid that? I want that actually the whole URL is passed correctly.

Upvotes: 0

Views: 110

Answers (2)

glenn jackman
glenn jackman

Reputation: 246807

I believe that the backticks execute their contained string with /bin/sh, so you probably want:

output = `casperjs myscript.js '#{url}'`

Upvotes: 1

usha
usha

Reputation: 29349

i think this will work

casperjs myscript.js "#{url}"

When i tested in irb, it worked for one of the url's you have provided here

Upvotes: 0

Related Questions