Skip to content

Instantly share code, notes, and snippets.

@mdespuits
Created May 3, 2012 15:34
Show Gist options
  • Save mdespuits/2586559 to your computer and use it in GitHub Desktop.
Save mdespuits/2586559 to your computer and use it in GitHub Desktop.
Add lvh.me to Mac OS X hosts file
#!/bin/bash
main() {
if [[ $(has_lvh_me) == 1 ]]; then
echo 'lvh.me is already specified in your hosts file'
else
add_lvh_me
echo 'lvh.me was added to your hosts file!'
fi
flush_dns_cache
}
has_lvh_me() {
if [[ $(find_lvh_in_hosts) -eq "1" ]]; then
echo 1
else
echo 0
fi
}
find_lvh_in_hosts() {
local has_lvh=`cat /etc/hosts | grep lvh.me | wc -l`
if [[ "$has_lvh" -gt "0" ]]; then
echo "1"
else
echo "0"
fi
}
add_lvh_me() {
sudo echo '127.0.0.1 lvh.me' >> /etc/hosts
}
flush_dns_cache() {
if [ `sysctl -n kern.osrelease | cut -d . -f 1` -lt 9 ]; then
lookupd -flushcache
else
dscacheutil -flushcache
fi
}
main
@mdespuits
Copy link
Author

The reason I created this was because somehow my lvh.me setup was destroyed in my /etc/hosts file, so this added it back in and made it all work again! (I think it may have had something to do with Pow, but I have not been able to confirm that).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment