[TASK] Add list mode

This commit is contained in:
Dominique Feyer 2011-11-16 11:20:25 +01:00
parent d1edb3dd30
commit ff7f431bd7
1 changed files with 15 additions and 6 deletions

21
host-manager Normal file → Executable file
View File

@ -2,10 +2,11 @@
# 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"
addusage="Usage: `basename $0` --add host address"
remusage="Usage: `basename $0` --remove host"
listusage="Usage: `basename $0` --list [127.]"
case "$1" in
-add)
--add)
if [ $# -eq 3 ]; then
if [[ -n $(grep "^$3.*[^A-Za-z0-9\.]$2$" ${path}) ]]; then
echo "Duplicate address/host combination, ${path} unchanged."
@ -16,14 +17,22 @@ case "$1" in
echo $addusage;
fi
;;
-remove)
--remove)
if [ $# -eq 2 ]; then
sed -i '' -e "s/^[^#].*[^A-Za-z0-9\.]$2$//g" -e "/^$/ d" ${path}
sed -i '' "s/^[^#].*[^A-Za-z0-9\.]$2$//g;/^$/d" ${path}
else
echo $remusage;
fi
;;
--list)
if [ $# -eq 2 ]; then
grep "^$2" ${path}
else
cat ${path} | grep -v "^#"
fi
;;
*)
echo $addusage;
echo $remusage;
esac
echo $listusage;
esac