[英]How to pass ruby variable into sed command in Shell?
This is my code. 这是我的代码。 I want to grab the p's value and insert it into the file changed.txt when matched 1. But it doesn't do what I want to, it seems it doesn't know what #{p} is
我想抓住p的值并在匹配1时将其插入到文件changed.txt中。但它没有做我想做的事情,似乎它不知道#{p}是什么
Net::SSH.start( "192.168.2.1", "root", :password => "password") do |ssh|
p = ssh.exec! "java -cp /var/lib/sonar/dev-tools.jar au.com.Tool test"
# puts #{p}
ssh.exec! "sed 's/1/#{p}/g' changed.txt"
end
The passing of the p
value the way you have it should work fine. 以你拥有它的方式传递
p
值应该可以正常工作。 However, the sed
command doesn't change the file. 但是,
sed
命令不会更改文件。 If you want it to change the file in place, use the -i
option like so: 如果您希望它在适当的位置更改文件,请使用
-i
选项,如下所示:
ssh.exec! "sed -i 's/1/#{p}/g' changed.txt"
Or if you want the changes in a different file, then use: 或者,如果您想要在其他文件中进行更改,请使用:
ssh.exec! "sed 's/1/#{p}/g' changed.txt > newfile.txt"
An alternative option would be: 另一种选择是:
ssh.exec! "sed -i 's/1/" + p + "/g' changed.txt"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.