28 lines
908 B
Bash
28 lines
908 B
Bash
removehost() {
|
|
hostname="$1"
|
|
|
|
if grep -qP "[[:space:]]$hostname" /etc/hosts; then
|
|
echo "$hostname found in /etc/hosts. Removing now..."
|
|
sudo sed -ie "/[[:space:]]$hostname/d" /etc/hosts || echo "cannot remove host from /etc/hosts" >&2
|
|
else
|
|
echo "$hostname was not found in /etc/hosts" >&2
|
|
fi
|
|
}
|
|
|
|
addhost() {
|
|
hostname="$1"
|
|
|
|
if grep -qP "[[:space:]]$hostname" /etc/hosts; then
|
|
echo "$hostname already exists: $(grep "$hostname" /etc/hosts)" >&2
|
|
else
|
|
echo "Adding $hostname to /etc/hosts..."
|
|
printf "%s\t%s\n" "127.0.0.1" "$hostname" | sudo tee -a /etc/hosts > /dev/null || echo "cannot add host to /etc/hosts" >&2
|
|
|
|
if grep -q "$hostname" /etc/hosts; then
|
|
echo "$hostname was added succesfully:"
|
|
grep "$hostname" /etc/hosts
|
|
else
|
|
echo "Failed to add $hostname" >&2
|
|
fi
|
|
fi
|
|
}
|