[英]Single quotes escaping Ruby
So, Im working with a Ruby script that needs to connect to a bunch of servers and get information from them. 因此,我使用的Ruby脚本需要连接到一堆服务器并从中获取信息。 The problem I am having is that the single quotes seem to be getting lost somehow. 我遇到的问题是单引号似乎以某种方式丢失了。 What am I doing wrong here ? 我在这里做错了什么?
command = "grep -E \'^(upstream| *server)\' /etc/nginx/upstreams.conf | sed -E \'s/_pool.*//g ; s/^upstream //g\'"
puts system("ssh -n -o 'StrictHostKeyChecking no' #{nginx_stage_servers[0]} #{command}")
$ ruby nx.rb
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `grep -E ^(upstream| *server) /etc/nginx/upstreams.conf'
true
The reason of the error is the single quotes missing. 错误的原因是缺少单引号。
You have too many layers of quoting and escaping to deal with when you use system(command_string)
, you're almost always better off using the multi-argument form of Kernel#system
to avoid dealing with the shell. 使用system(command_string)
,要处理的引号和转义符层太多,因此,最好总是使用Kernel#system
的多参数形式来避免处理shell。 Something like this will be less problematic: 这样的事情会减少一些问题:
system('ssh', '-n', '-o', 'StrictHostKeyChecking no', nginx_stage_servers[0], command)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.