简体   繁体   中英

Properly escaping shell arguments in Ruby

Here is the code:

desc "Find 301 errors on production servers"
task :find301 do
  command = "grep 'HTTP/1.1\" 301' /var/log/httpd/*ssl-access.log | grep -v 'gclid' | awk '{print \$7}' | sort | uniq -c | sort -nr"

  production_servers.each do |server|
    sh "ssh root@#{server} #{command}"
  end
end

Is there a better way to escape the shell command, preferably in a format that allows sh "ssh root@#{server} #{command}" to be used with arbitrary command s?

使用require 'shellwords'Shellwords.escape您可以在“ shellescape ”和“ Shellwords ”中找到有关这些的更多信息。

您可以使用%q%Q版本,但仍需要指定定界符。

%Q|these don't need to be escaped /\"'"'{}[]-_+=()!@#$%^&*`~;:<>.,|

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM