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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s