Skip to content

Instantly share code, notes, and snippets.

@nothingatalldotnet
Created April 1, 2016 10:17
Show Gist options
  • Save nothingatalldotnet/21b880408ffe3275cb5637d03d3ee02d to your computer and use it in GitHub Desktop.
Save nothingatalldotnet/21b880408ffe3275cb5637d03d3ee02d to your computer and use it in GitHub Desktop.
Bash script to add a line to osx hosts file
#!/bin/sh
ETC_HOSTS=/etc/hosts
IP="xx.xx.xx.xx"
HOSTNAME="www.domain.com"
HOSTS_LINE="$IP\t$HOSTNAME"
if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
then
echo "$HOSTNAME already exists : $(grep $HOSTNAME $ETC_HOSTS)"
else
echo "Adding $HOSTNAME to your $ETC_HOSTS";
sudo -- sh -c -e "echo '$HOSTS_LINE' >> /etc/hosts";
if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
then
echo "$HOSTNAME was added succesfully \n $(grep $HOSTNAME /etc/hosts)";
else
echo "Failed to Add $HOSTNAME, Try again!";
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment