Injecting local IP Address into a file with bash

bash

Sometimes you need to change an IP address inside config files to that of your current dev machine. Here's how I fix that with a bash script.

I was working on my project where the "standard" tomcat config checked in the project's repo wouldn't work on my mac.

A good place to put bash scripts

I have a bin folder inside my user folder. It is in the $PATH, meaning every script file in there can be executed as a script from anywhere. In there I create a text file called fix-config-PROJECT. Yes, I make one per project, because I hard code the paths of the files to change. This could evolve into something more complex if the need arises, but the setup I have so far has served me well so far.

nano ~/bin/fix-config-PROJECT

The script file

#!/bin/bash

IP=`ifconfig  | grep inet | grep -v inet6 | tail -n1 | awk '{print $2}'`
for file in "launchpad/src/main/tomcat/conf/context.xml" "path/to/etc.xml"
do
  svn revert $file
  sed -i "" "s/localhost/$IP/g" $file
done

Script breakdown

IP=ifconfig | grep inet | grep -v inet6 | tail -n1 | awk '{print $2}'
This runs a series of commands one after the other, using | to pass the result of each command to the next. The ` at the beginning and end put the result into a variable $IP
ifconfig
From the manpage: "The ifconfig utility is used to assign an address to a network interface and/or configure network interface parameters". Used without parameters, it returns a big chunk of text with current settings. The IP address is in there somewhere.
grep inet
grep -v inet6
extracts the lines from that chunk of text which have "inet" in it, but not "inet6"
tail -n1
take the last of these lines
awk '{print $2}'
At this point we have a string like inet 192.168.2.108 netmask 0xffffff00 broadcast 192.168.2.255. Awk can be used to extract the 192... from it. Awk is a utilities that processes a line of text by splitting into fields where blank spaces are - it is used for handling database dumps etc. In this case, it is basically saying that after it's split the line into fields (which it does automatically), it should print out the second field and throw away the rest.
for file in "launchpad/src/main/tomcat/conf/context.xml" "path/to/etc.xml"
The files I need to process are listed out one after the other. This is good enough for my use case. Note that the paths are not absolute, so that I can use the same command in different svn branches. What this line does is looping through each file
svn revert $file
sed -i "" "s/localhost/$IP/g" $file
First I revert the latest changes, then I make new ones. This time using sed, a utility which processes text a line at the time, transforming it on the way. The -i "" means to process the file in place (as opposed to create a copy), without creating a backup. The substitution is simple, I simply change all occurrences of locahost with the IP address from earlier

Running the script

I can now run the script from anywhere on my machine, usually as

cd PROJECT/branch_x
fix-config-PROJECT

and then all the config files are changed as required, and I can access my local version site from tablets etc using the IP address. It's a hack but it works.