LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 10-03-2012, 01:58 AM   #1
muhamed.ahmovic
LQ Newbie
 
Registered: Sep 2012
Location: Bosnia and Hercegovina
Distribution: CentOS
Posts: 21

Rep: Reputation: Disabled
how to sed file path


Hi,

i have tried everything and searched everyware but could not find solution for my problem.

I need to pass file path using sed. My code looks like this:

fpath=$(zenity --file-selection --text"Select client certificate" --save --filename="/etc/pki/CA/certs/")

ssh root@IP_ADDRESS 'sed -i '8s/.*/client_cert=\\\"$fpath\\\"/' /home/username/Desktop/fpath'

so, 8th line of destination file should look like this:

client_cert="/etc/pki/CA/certs/selected_file_name"

Please notice i have to insert double-quotes too.

Is it possible to accomplish this usig sed?


Thank you in advance and best regards

Last edited by muhamed.ahmovic; 10-03-2012 at 02:13 AM.
 
Old 10-03-2012, 03:05 AM   #2
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,188

Rep: Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184
Assuming your zenity command echos the correct path and hence sets fpath to /etc/pki/CA/certs/selected_file_name as you suspect, then I think this would probably work:

Code:
ssh root@IP_ADDRESS "sed -i 8s#.*#client_cert=\\\"${fpath}\\\"# /home/username/Desktop/fpath"
 
1 members found this post helpful.
Old 10-03-2012, 03:06 AM   #3
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
I am not sure exactly what you want to do.
- Does your ssh command line work perfectly if you give it a correctly-formed file without the sed subcommmand? Can we ignore that?
- And does your zenity command produce the right information? In which case we can ignore that too.

So your problem is to edit an existing file in-place so that its 8th line becomes client_cert="$fpath"
To get shell variable substitution, you need double quotes not single quotes. Does this work:
Code:
sed "8s/.*/client_cert=\"$fpath\"/" /home/username/Desktop/fpath
 
Old 10-03-2012, 03:33 AM   #4
muhamed.ahmovic
LQ Newbie
 
Registered: Sep 2012
Location: Bosnia and Hercegovina
Distribution: CentOS
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by SecretCode View Post
I am not sure exactly what you want to do.
- Does your ssh command line work perfectly if you give it a correctly-formed file without the sed subcommmand? Can we ignore that?
- And does your zenity command produce the right information? In which case we can ignore that too.

So your problem is to edit an existing file in-place so that its 8th line becomes client_cert="$fpath"
To get shell variable substitution, you need double quotes not single quotes. Does this work:
Code:
sed "8s/.*/client_cert=\"$fpath\"/" /home/username/Desktop/fpath
ssh command works, zenity produce right information (variable). Problem is in inserting variable $fpath in file on remote machine.

tnx for answer i will try your code...

Regards
 
Old 10-03-2012, 03:34 AM   #5
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,188

Rep: Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184
@SecretCode: No your command will not work. Did you actually try testing it first?

Keep in mind he is also sending the command over ssh, so further quotes are needed around the whole sed command and hence you need more escaping around the the \ and ". Additionally as he is inserting a path, which will contain /'s, he needs to use something else for separation, e.g. #.

You can simulate the complexity of his setup if you have a local ssh server running. To do so make a file called fpathtest in your home directory and give it at least 8 lines, with the final line being something like:

Code:
client_cert="/test/path"
Then issue the following:

Code:
export="/etc/pki/CA/certs/selected_file_name"
Next try running your sample command, e.g. in my case I tested my command as follows:

Code:
ssh localhost "sed -i 8s#.*#client_cert=\\\"${fpath}\\\"# fpathtest"
I was able to confirm fpathtest updated correctly by reading it in less afterwards and noting that the 8th line read:

Code:
client_cert="/etc/pki/CA/certs/selected_file_name"

Last edited by ruario; 10-03-2012 at 03:36 AM.
 
Old 10-03-2012, 03:41 AM   #6
muhamed.ahmovic
LQ Newbie
 
Registered: Sep 2012
Location: Bosnia and Hercegovina
Distribution: CentOS
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ruario View Post
Assuming your zenity command echos the correct path and hence sets fpath to /etc/pki/CA/certs/selected_file_name as you suspect, then I think this would probably work:

Code:
ssh root@IP_ADDRESS "sed -i 8s#.*#client_cert=\\\"${fpath}\\\"# /home/username/Desktop/fpath"
Works perfect!!!!

Thank you so much m8

Best regards
 
Old 10-03-2012, 03:48 AM   #7
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by ruario View Post
@SecretCode: No your command will not work. Did you actually try testing it first?
Yes I did, but I misread the situation as passing a local file as a config file to ssh. Sorry.
 
Old 10-03-2012, 06:11 AM   #8
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,188

Rep: Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184Reputation: 1184
Quote:
Originally Posted by muhamed.ahmovic View Post
Works perfect!!!!
Fantastic!

Quote:
Originally Posted by SecretCode View Post
Yes I did, but I misread the situation as passing a local file as a config file to ssh. Sorry.
Actually I should probably say sorry to you. Reading my comment again I just realised it comes off a little rudely. That actually wasn't intentional. We are all trying to help here so just to be clear, I wasn't trying to offend and certainly no need for you to apologise!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
[SOLVED] How to get the absolute path of the running executable file and current work path ? 915086731 Programming 3 11-01-2011 09:28 AM
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
how to use script sed with full path azeus Programming 8 03-14-2008 05:23 AM
how to remove path using sed dtcs Programming 2 12-25-2006 04:29 PM

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

All times are GMT -5. The time now is 08:51 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration