LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Edit file line on remote machine (https://www.linuxquestions.org/questions/linux-newbie-8/edit-file-line-on-remote-machine-4175429554/)

muhamed.ahmovic 09-29-2012 03:38 AM

Edit file line on remote machine
 
Hi,

after few days of trying i have decided to ask for help. My problem is following, i am writing script for editing specific line in specific file on remote machine using ssh. Problem is i cant pass double quotes in my variable. To be clear, here is the command i use to edit file:
-before script comes to this line i have exported variable called $w-
sshpass -p 'PASSWORD' ssh root@IP_ADDRESS -o StrictHostKeyChecking=no 'sed -i '12s/.*/few_words="'$w'"/' /path/to/file
Now, everything works fine but i can not get double quotes in remote file. Result should be few_words="variable", what i get is few_words=variable. When i execute this command locally it works ( i get few_words="variable"), but in script it does not.

Thanks in advance...

pingu 09-29-2012 09:16 AM

A few things to try:
1) Escape the double-quote:
Code:

sed -i '12s/.*/few_words=\"'$w'\"/' /path/to/file
2) Put the command in a file on remote machine, make it executable and call that file via ssh.

3) Add an extra line substituting "few_words=variable" few_words="variable"

muhamed.ahmovic 09-29-2012 11:55 AM

[QUOTE=pingu;4792516]A few things to try:
1) Escape the double-quote:
Code:

sed -i '12s/.*/few_words=\"'$w'\"/' /path/to/file
this does not work

2) Put the command in a file on remote machine, make it executable and call that file via ssh.

yes, this is solution (i have tried it works), but i can't believe it is not possible to send double-quotes in variable via ssh?
i mean extra script just for double-quotes???

Thank you and best regards m8

Elv13 09-29-2012 01:30 PM

Code:

ssh user@localhost echo '\"asdsd\"'
work fine for me

muhamed.ahmovic 09-29-2012 02:23 PM

no, it is not working for me...

tnx

colucix 09-29-2012 02:25 PM

You have to escape the double quotes, but you must protect both the escape character and the double quotes from the local shell. Example:
Code:

ssh user@host "sed -i '12s/.*/few_words=\\\"$w\\\"/' /path/to/file"
Don't worry about the single quotes that embed the sed command, since the w variable is expanded by the local shell and it is its literal value that travels to the remote machine.

muhamed.ahmovic 09-29-2012 02:28 PM

yesssss!!!!

Thank you so much colucix, you are my savior!

Respect and best regards m8

colucix 09-29-2012 02:28 PM

You're welcome! :)


All times are GMT -5. The time now is 12:45 AM.