Help :: Simple Shell Scripting

Hello,

I want to find the "IP-OF-SERVER" in /etc/squid/squid.conf And replace it with The IP of server.
I know this command returns the IP of server :

ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

And I can replace with sed. :

sed -i 's/IP-OF-SERVER/#REPLACEMENT/g' /etc/squid/squid.conf

How I can combine the above commands together so that it replaces IP-OF-SERVER phrase with the ip of server automatically ?
Sometimes the code :

ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 |  awk '{ print $1}'

returns more than 1 ip if the server has more than 1 ip.
I just want to replace IP-OF-SERVER phrase with the first ip in list.

Best Regards

Ad hoc (untested):

REP=$( ifconfig  | grep 'inet addr:' | grep -v '127.0.0.1' | head -n1 | cut -d: -f2 |  awk '{ print $1}' ) ; \
  sed -i /etc/squid/squid.conf -e "s/IP-OF-SERVER/$REP/" 

Hello,

Thanks! Works great !

  • Why you have removed /g in sed command ?
  • Is it possible to make this command in one line ? such this :
sed -i /etc/squid/squid.conf -e "s/IP-OF-SERVER/$REP/" | REP=$( ifconfig  | grep 'inet addr:' | grep -v '127.0.0.1' | head -n1 |  cut -d: -f2 |  awk '{ print $1}' ) ;

Consider it a typo of sorts :rolleyes:

Is it possible to run these commands in one line ?
I need them to use on openvz os template but it just accept one-line command.