2013-11-20 31 views
0

我在我的Rails應用程序下面一行:'SH:1:語法錯誤:未終止的引號的字符串' 在Ruby中啓動一個bash腳本

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 [:頁面] [:URL]包含了單引號,例如像這個網址:http://www.degree33surfboards.com/surf-gear/ultimate-9'-epoxy-sand-.html

我得到這個錯誤:

sh: 1: Syntax error: Unterminated quoted string 

我怎樣才能避免這樣的事情發生?

回答

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 
) 
相關問題