Question

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

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
Was it helpful?

Solution

The passing of the p value the way you have it should work fine. However, the sed command doesn't change the file. If you want it to change the file in place, use the -i option like so:

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"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top