By connecting via ssh, you must run the script on the remote machine. Can't figure out sed. Or rather, how it interacts with the transmitted variables

Example:

#!/bin/bash # A=«java.extended.prop=-XX\:NewSize\=1024m -XX\:MaxNewSize\=1024m -XX\:PretenureSizeThreshold\=10m» ssh root@10.2.200.172 " B=\$(echo $A | cut -d «=» -f 2-) touch /tmp/test.txt echo «java.extended» > /tmp/test.txt cat /tmp/test.txt sed -i '/java/a \$B' /tmp/test.txt cat /tmp/test.txt " 

The result of the work will be as follows:

java.extended $ B

How to correctly transfer sed to a variable computed on a remote machine in order to get the record I need?

What should happen:

java.extended -XX: NewSize \ = 1024m -XX: MaxNewSize \ = 1024m -XX: PretenureSizeThreshold \ = 10m

  • you have "gone" markup in question. I restored it very roughly, but only you are fully aware of your idea. Correct the issue by clicking edit below the text of the question. I recommend reading the formatting help section - aleksandr barakin
  • @ aleks.andr Thank you very much! - Nicolas Chabanovsky

2 answers 2

You can write the result of executing a script / command on a remote machine into a variable on a local one, and then process this result on a local machine.

 FIRST_VAR="very long text" SECOND_VAR=`ssh username@servername "some_remote_script $FIRST_VAR ...."` echo $SECOND_VAR | sed ... 

Try this.

    You need to transfer the result of the script execution on the target machine to the machine initiating the ssh connection, right?

    Then you can redirect the output of your script to a file, and then get this file back like this:

     scp local/path/to/file/file_name user@host:/remote/path/to/save/file 

    Man scp here .

    PS: maybe with all this you will have to set up ssh authentication using a public key ( More on ssh-keygen).