hosts/hosts

38 lines
807 B
Plaintext
Raw Normal View History

2011-11-15 22:06:00 +00:00
#!/bin/bash
# Idea and interface taken from https://github.com/macmade/host-manager
2011-11-15 22:06:53 +00:00
path="/etc/hosts"
2011-11-16 10:20:25 +00:00
addusage="Usage: `basename $0` --add host address"
remusage="Usage: `basename $0` --remove host"
listusage="Usage: `basename $0` --list [127.]"
2011-11-15 22:06:00 +00:00
case "$1" in
2011-11-16 10:20:25 +00:00
--add)
2011-11-15 22:06:00 +00:00
if [ $# -eq 3 ]; then
2011-11-15 22:19:15 +00:00
if [[ -n $(grep "^$3.*[^A-Za-z0-9\.]$2$" ${path}) ]]; then
2011-11-15 22:15:57 +00:00
echo "Duplicate address/host combination, ${path} unchanged."
else
printf "$3\t$2\n" >> ${path}
fi
2011-11-15 22:06:00 +00:00
else
echo $addusage;
fi
;;
2011-11-16 10:20:25 +00:00
--remove)
2011-11-15 22:06:00 +00:00
if [ $# -eq 2 ]; then
2011-11-16 10:20:25 +00:00
sed -i '' "s/^[^#].*[^A-Za-z0-9\.]$2$//g;/^$/d" ${path}
2011-11-15 22:06:00 +00:00
else
echo $remusage;
fi
;;
2011-11-16 10:20:25 +00:00
--list)
if [ $# -eq 2 ]; then
grep "^$2" ${path}
else
cat ${path} | grep -v "^#"
fi
;;
2011-11-15 22:06:00 +00:00
*)
echo $addusage;
echo $remusage;
2011-11-16 10:20:25 +00:00
echo $listusage;
esac