Ruby 在反引号 Shell 中转义参数

标签 ruby shell process escaping

我想将我用 open-uri 和 nokogiri 抓取的 HTML 文件填充的 Ruby 变量放入反引号系统进程中以对其进行整理。变量的性质混淆了过程。我想我需要逃避它,但我不确定。任何建议表示赞赏。

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

这是我得到的错误:

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");'>'

干杯

最佳答案

您可能想改用 IO.popen。然后您可以使用数组调用命令而不是将其字符串化:

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
}

假设 tidy 从标准输入读取 HTML。

关于Ruby 在反引号 Shell 中转义参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27623072/

相关文章:

ruby-on-rails - 我应该测试模型之间的关联吗?

linux环境变量diff

shell - 终端 cd 命令不适用于 Scala 脚本

java - 如何告诉父进程等待子进程?

python - 一分钟内创建了多少进程?

java - 等待 Java 进程的答复?

ruby - 初学者数组迭代和错误代码解释

ruby - 跨不同工厂共享特征

ruby - 从源安装 riak 时出错

shell - shell脚本中的间接参数替换