Full Stack Software Developer

Host file updater

Background:

If you are a developer managing multiple virtual hosts on your system you probably have to update the /etc/hosts file quite a lot. This script gives a easy to use way to add and remove domain from the etc file.

Code:

Create a new file named “hostupdater.sh” then paste and save the code.

#!/bin/sh
# This script was developed by faisal ahmed and is provided as is without any warranty.

if grep "$2$" /etc/hosts; then
  if [ "$1" = "del" ]; then
    sed -i "/.*$2$/d" /etc/hosts
  else
    sed -i "s/.*$2$/$1 $2/g" /etc/hosts
  fi
else
  echo $1 $2 >> /home/vagrant/hosts
fi

Usage:

To add a new site type

./hostupdater.sh 192.168.33.12 test1.com

To remove the site type

./hostupdater.sh del test1.com

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.