简体   繁体   中英

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. 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}")

Error I am getting :

 $ 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. Something like this will be less problematic:

system('ssh', '-n', '-o', 'StrictHostKeyChecking no', nginx_stage_servers[0], command)

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