LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 02:58 PM   #1
muhamed.ahmovic
LQ Newbie
 
Registered: Sep 2012
Location: Bosnia and Hercegovina
Distribution: CentOS
Posts: 21

Rep: Reputation: Disabled
sed spaces in variable


Hi,

i am trying to write script which will update remote machine /etc/hosts file.
My code looks like this:
srv_ip=$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}')
srv_name=$(hostname)
srv="$srv_ip $srv_name"
ssh root@ip_addres 'sed -i '3s/.*/$srv/' /etc/hosts'

Script doesn't do anything.
Tried to send $srv_ip - it works, tried to send $srv_name - it works.
I realized that problem is in space inside variable.
Is there any way to accomplish this?

Thank you in advance and best regards
 
Old 10-03-2012, 03:16 PM   #2
muhamed.ahmovic
LQ Newbie
 
Registered: Sep 2012
Location: Bosnia and Hercegovina
Distribution: CentOS
Posts: 21

Original Poster
Rep: Reputation: Disabled
got it:
.
.
.
.

ssh root@ip_address 'sed -i '3s/.*/"'${mgmt_ip} ${mgmt_name}'"/' /etc/hosts'
 
Old 10-04-2012, 02:42 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Please use ***[code][/code] tags*** around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


The solution you posted leaves the variables completely open to the shell. A better way is to start by surrounding the entire expression in double-quotes. Then, when you need literal quotes inside the string, prefix them with backslashes.


Code:
ssh root@ip_address "sed -i \"3s/.*/$mgmt_ip $mgmt_name\" /etc/hosts"
It's important to properly understand how the shell processes arguments and whitespace.
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

And see the bash man page QUOTING section for more on how escapes are handled in double-quotes.

(I suggest not using curly braces on variables either if not necessary. It does nothing but clutter up the code.)


Next, this is rather ugly:

Code:
srv_ip=$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}')
It's very rare that you should need to use cut, grep, sed, and awk in combination. awk in particular is a full featured text-processing language capable of doing all the work of the others.

Code:
srv_ip=$( ifconfig eth0 | awk -F'[: ]+' '/inet addr/ { print $4 }' )
You may have to adjust the field number if your ifconfig produces different output from mine.

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/


Oh yeah, and bash, at least, has a built-in $HOSTNAME variable you can use instead of the command of the same name.
 
1 members found this post helpful.
Old 10-05-2012, 02:10 AM   #4
muhamed.ahmovic
LQ Newbie
 
Registered: Sep 2012
Location: Bosnia and Hercegovina
Distribution: CentOS
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thank you for making things clear, and for references...

Best regards
 
  


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 with variable that contains spaces? mp85 Linux - Newbie 4 08-29-2012 12:25 PM
[SOLVED] SED - how to remove blank spaces carolflb Linux - Newbie 2 01-30-2010 07:02 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 04:44 PM
Escaping spaces in a variable Ransak Programming 3 02-10-2008 02:54 AM
How to add some spaces in sed command? reconcil Linux - Newbie 2 11-05-2007 12:00 PM


All times are GMT -5. The time now is 11:59 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration