-
-
Save mikejw/be56e4bba2f26ff2de57 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# Idea and interface taken from https://github.com/macmade/host-manager | |
path="/etc/hosts" | |
addusage="Usage: `basename $0` -add host address" | |
remusage="Usage: `basename $0` -remove host" | |
case "$1" in | |
-add) | |
if [ $# -eq 3 ]; then | |
if [[ -n $(grep "^$3.*[^A-Za-z0-9\.]$2$" ${path}) ]]; then | |
echo "Duplicate address/host combination, ${path} unchanged." | |
else | |
printf "$3\t$2\n" >> ${path} | |
fi | |
else | |
echo $addusage; | |
fi | |
;; | |
-remove) | |
if [ $# -eq 2 ]; then | |
sed -i '' -e "s/^[^#].*[^A-Za-z0-9\.]$2$//g" -e "/^$/ d" ${path} | |
else | |
echo $remusage; | |
fi | |
;; | |
*) | |
echo $addusage; | |
echo $remusage; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment