Friday, April 22, 2016

Bash scripting - replace URLs for different environments

Unfortunately, due to limitations of the project, a better solution for environment specific settings was not available.  I wrote a bash script to perform automated deployment of a front end angular web application.  There was a client-side javascript file that needed a specific environment specific URL.

Below is the sed command I used inside the bash script to do this.  Instead of using a backslash as the delimiter, I used #, you can use another non-character as well.

Environments tested
CentOS release 6.7

To replace "https://localhost:8080" with "http://dev.cherryshoetech.com" in the cherryshoetech.js file:
  • When you don't need to use shell variables, the following works:
sed -i 's#https://localhost:8080#http://dev.cherryshoetech.com#g' cherryshoetech.js
  • When you need to use shell variables, the following works.  Notice the double quotes are used for interpolation:
sed -i "s#$OLD_ENVIRONMENT_URL#$NEW_ENVIRONMENT_URL#g" cherryshoetech.js

Also used the following to grep for specific process "gulp" and kill in the same line.  This article was of great help.
kill $(ps aux | grep '[g]ulp' | awk '{print $2}')


No comments:

Post a Comment

I appreciate your time in leaving a comment!