title: Using sed to change ip addresses in dhcpcd.conf file
body: <p>On a raspberry pi I am trying to write a simple script that will allow me to change the static ip settings in the dhcpcd.conf file.  The below script works except for the dns-servers.  It appears that the sed statement does not work for that line as it contains two ip addresses seperated by a space.  The script is as follows:</p>
  
  <pre><code>#!/bin/bash
  currip=$(cat /etc/dhcpcd.conf | grep -e '^static ip_address=' | cut -d= -
  f2)
  currgw=$(cat /etc/dhcpcd.conf | grep -e '^static routers=' | cut -d= -f2)
  currdns=$(cat /etc/dhcpcd.conf | grep -e '^static domain_name_servers=' | 
  cut -d= -f2)
  echo "current IP is $currip"
  echo "current GW is $currgw"
  echo "current DNS servers are $currdns"
  echo "Enter new static ip in form of x.x.x.x/x: "
  read newip
  echo "Enter new GW in form of x.x.x.x: "
  read newgw
  echo "Enter new DNS servers in form of x.x.x.x x.x.x.x: "
  read newdns
  echo "currip is $currip"
  echo "new ip will be $newip"
  echo "new dns will be $newdns"
  sed -i -e "s@$currip\b@$newip@g" /etc/dhcpcd.conf
  sed -i -e "s@$currgw\b@$newgw@g" /etc/dhcpcd.conf
  sed -i -e "s@$currdns\b@$newdns@g" /etc/dhcpcd.conf
  chip=$(cat /etc/dhcpcd.conf | grep -e '^static ip_address=' | cut -d= -
  f2)
  chgw=$(cat /etc/dhcpcd.conf | grep -e '^static routers=' | cut -d= -f2)
  chdns=$(cat /etc/dhcpcd.conf | grep -e '^static domain_name_servers=' | 
  cut -d= -f2)
  echo "The ip has been changed to $chip"
  echo "The GW has been changed to $chgw"
  echo "The DNS server have been changed to $chdns"
  </code></pre>
  
  <p>The lines in the dhcpcd.conf file look like this:</p>
  
  <p>static ip_address=192.168.126.7/24</p>
  
  <p>static routers=192.168.126.1</p>
  
  <p>static domain_name_servers=192.168.126.1 66.243.243.101</p>
  
  <p>How do I need to tweak my sed statement for the domain_name_servers?</p>