我的Rails应用程序中有以下行:
pid = Process.spawn("casperjs #{path_to_file} '#{params[:page][:url]}' '#{Rails.root}/tmp/' --ignore-ssl-errors=yes", :out => pipe_cmd_out, :err => pipe_cmd_out)
如果params [:page] [:url]包含单引号,例如此网址:http://www.degree33surfboards.com/surf-gear/ultimate-9'-epoxy-sand-.html
我收到此错误:
sh: 1: Syntax error: Unterminated quoted string
我怎样才能避免这种情况发生?
答案 0 :(得分:1)
使用Process.spawn
的参数列表形式,而不是尝试引用shell的内容。
Process.spawn(
"casperjs", path_to_file, params[:page][:url],
"#{Rails.root}/tmp/", "--ignore-ssl-errors=yes",
:out => pipe_cmd_out,
:err => pipe_cmd_out
)