Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Escaping Arguments Inside Backticks Shell

I'm wanting to throw a Ruby variable filled with an HTML file that I have grabbed with open-uri and nokogiri, into a backticks system process to tidy it up. The nature of the variable is confusing the process. I am thinking I need to escape it but I am not sure. Any advice appreciated.

require 'open-uri'
require 'nokogiri'

url = 'http://www.wikihow.com/Bathe-a-Cat'
page = Nokogiri::HTML(open(url))
pagestring = page.to_s

result = `tidy --break-before-br no --char-encoding utf8 --clean yes --drop-empty-paras yes ' #{pagestring}'`
puts results.length

Here is the error I get:

sh: -c: line 144: syntax error near unexpected token `"Search","Search","Custom_search"'
sh: -c: line 144: `    <input type="submit" id="cse_sa" value="Search" class="search_button" onmouseover="button_swap(this);" onmouseout="button_unswap(this);" onclick='gatTrack("Search","Search","Custom_search");'>'

Cheers

like image 917
Rich_F Avatar asked Apr 22 '26 15:04

Rich_F


2 Answers

For normal arguments like file paths and stuff like that, you could use "str".shellescape (http://apidock.com/ruby/Shellwords/shellescape).

args_array = [ ... ]
`tidy #{args_array.map(&:shellescape).join(' ')`

However, to pass a complete html file as an command line argument, something like what was suggested above might be better. I just though I'd mention this here for reference to others for normal cli arguments.

like image 146
Ernesto Avatar answered Apr 24 '26 06:04

Ernesto


You might want to use IO.popen instead. Then you can invoke the command with an array instead of stringifying it:

cmd = %w{ tidy --break-before-br no --char-encoding utf8 --clean yes --drop-empty-paras yes }
result = IO.popen(cmd, 'r+') {|io| 
  io.puts pagestring
  io.close_write
  io.read
}

assuming tidy reads HTML from stdin.

like image 43
glenn jackman Avatar answered Apr 24 '26 07:04

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!