updated stuff

This commit is contained in:
Nikolas Weger 2020-01-31 13:59:45 +01:00
parent 2a201e89ba
commit d0f6bd7c9a

View file

@ -3,9 +3,6 @@
# Path to your hosts file
hostsFile="/etc/hosts"
# Default IP address for host
ip="127.0.0.1"
# Hostname to add/remove.
hostname="$2"
@ -14,7 +11,7 @@ die() { yell "$*"; exit 111; }
try() { "$@" || die "cannot $*"; }
remove() {
if grep -qP "[[:space:]]$hostname" /etc/hosts; then
if grep -qP "[[:space:]]$hostname" "$hostsFile"; then
echo "$hostname found in $hostsFile. Removing now..."
try sudo sed -ie "/[[:space:]]$hostname/d" "$hostsFile"
else
@ -23,15 +20,15 @@ remove() {
}
add() {
if grep -qP "[[:space:]]$hostname" /etc/hosts; then
if grep -qP "[[:space:]]$hostname" "$hostsFile"; then
yell "$hostname already exists: $(grep "$hostname" $hostsFile)"
else
echo "Adding $hostname to $hostsFile..."
try printf "%s\t%s\n" "$ip" "$hostname" | sudo tee -a "$hostsFile" > /dev/null
try printf "%s\t%s\n" "127.0.0.1" "$hostname" | sudo tee -a "$hostsFile" > /dev/null
if grep -q "$hostname" /etc/hosts; then
if grep -q "$hostname" "$hostsFile"; then
echo "$hostname was added succesfully:"
grep "$hostname" /etc/hosts
grep "$hostname" "$hostsFile"
else
die "Failed to add $hostname"
fi