LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-29-2012, 03:38 AM   #1
muhamed.ahmovic
LQ Newbie
 
Registered: Sep 2012
Location: Bosnia and Hercegovina
Distribution: CentOS
Posts: 21

Rep: Reputation: Disabled
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...
 
Old 09-29-2012, 09:16 AM   #2
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Rep: Reputation: 127Reputation: 127
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"
 
Old 09-29-2012, 11:55 AM   #3
muhamed.ahmovic
LQ Newbie
 
Registered: Sep 2012
Location: Bosnia and Hercegovina
Distribution: CentOS
Posts: 21

Original Poster
Rep: Reputation: Disabled
[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
 
Old 09-29-2012, 01:30 PM   #4
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 129Reputation: 129
Code:
ssh user@localhost echo '\"asdsd\"'
work fine for me
 
Old 09-29-2012, 02:23 PM   #5
muhamed.ahmovic
LQ Newbie
 
Registered: Sep 2012
Location: Bosnia and Hercegovina
Distribution: CentOS
Posts: 21

Original Poster
Rep: Reputation: Disabled
no, it is not working for me...

tnx
 
Old 09-29-2012, 02:25 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
1 members found this post helpful.
Old 09-29-2012, 02:28 PM   #7
muhamed.ahmovic
LQ Newbie
 
Registered: Sep 2012
Location: Bosnia and Hercegovina
Distribution: CentOS
Posts: 21

Original Poster
Rep: Reputation: Disabled
yesssss!!!!

Thank you so much colucix, you are my savior!

Respect and best regards m8
 
Old 09-29-2012, 02:28 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're welcome!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to edit a line from a file? mostofas Ubuntu 6 02-02-2012 11:15 PM
How to change a line in a file and send to a remote machine with rsync danndp Linux - Server 4 11-20-2010 11:34 AM
how to edit remote file on linux ? kapilbajpai88 Linux - Newbie 5 09-05-2008 09:54 AM
scp: copy a file from local machine to remote machine seran Linux - Newbie 8 10-30-2007 12:23 PM
how to edit a file when not in X (ie from the command line) ludwig W Linux - Newbie 12 04-22-2003 04:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration