Skip to content

Instantly share code, notes, and snippets.

@ricklancee
Forked from nddrylliog/host-manager
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save ricklancee/c3415805f3c1e6b5203b to your computer and use it in GitHub Desktop.

Select an option

Save ricklancee/c3415805f3c1e6b5203b to your computer and use it in GitHub Desktop.
Easily edit your /etc/host file
#!/bin/bash
_hosts() {
hostfile="/etc/hosts"
addusage="Usage: `basename $0` --add address host"
remusage="Usage: `basename $0` --remove host"
case "$1" in
--add)
if [ $# -eq 3 ]; then
if [[ -n $(grep "^$2.*[^A-Za-z0-9\.]$3$" ${hostfile}) ]]; then
echo "Duplicate address/host combination, ${hostfile} unchanged."
else
printf "$2\t$3\n" >> ${hostfile}
fi
else
echo $addusage;
fi
;;
--remove)
if [ $# -eq 2 ]; then
sudo sh -c "sed -i '' -e 's/^[^#].*[^A-Za-z0-9\.]$2$//g' -e '/^$/ d' ${hostfile}"
else
echo $remusage;
fi
;;
*)
echo $addusage;
echo $remusage;
esac
}
alias hosts=_hosts
@ricklancee
Copy link
Author

usage

hosts --add 127.0.0.1 myapp
hosts --remove myapp

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