Skip to content

Instantly share code, notes, and snippets.

@adamnorwood
Created May 9, 2013 15:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamnorwood/5548102 to your computer and use it in GitHub Desktop.
Save adamnorwood/5548102 to your computer and use it in GitHub Desktop.
A simple bash script for setting up a local directory and corresponding entry in /etc/hosts, suitable for use with Apache wildcard name-based virtual hosts. For example: 'newsite example.dev' creates a directory called /www/example.dev/ and also adds appropriate 127.0.0.1 / ::1 entries for example.dev to /etc/hosts (the latter step requires sudo).
#!/bin/bash
DIRECTORY="/www/$1"
DIR_CREATED=false
HOSTS_EDITED=false
if [ -d "$DIRECTORY" ]; then
echo "* Directory $DIRECTORY already exists, skipping this step."
else
mkdir $DIRECTORY
DIR_CREATED=true
echo "* Directory added: $DIRECTORY"
fi
if grep -Fq "$1" /etc/hosts; then
echo "* Site already exists in /etc/hosts, skipping this step."
else
echo -e "\n127.0.0.1\t$1\n::1\t\t$1" | sudo tee -a /etc/hosts > /dev/null
HOSTS_EDITED=true
echo "* Site added to /etc/hosts"
fi
if $DIR_CREATED || $HOSTS_EDITED; then
echo -e "\nSite added: http://$1/"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment