gpt4 book ai didi

ruby - 在 Ruby 中正确转义 shell 参数

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:52 24 4
gpt4 key购买 nike

代码如下:

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

是否有更好的方法来转义 shell 命令,最好是采用允许 sh "ssh root@#{server} #{command}" 与任意 命令一起使用的格式是什么?

最佳答案

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

关于ruby - 在 Ruby 中正确转义 shell 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28307289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com