-
-
Save ricklancee/c3415805f3c1e6b5203b to your computer and use it in GitHub Desktop.
Easily edit your /etc/host file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage
hosts --add 127.0.0.1 myapphosts --remove myapp