Removing hostnames from /etc/hosts with Bash
by crothman from LinuxQuestions.org on (#4Z5Y3)
Hello,
I'm trying to remove hostnames beginning with ^nas and ^fas from /etc/hosts but for some reason, it's not working. I think in this script, I either added something or needs to be removed. Here is my script:
#!/bin/sh -x
ETC_HOSTS=/etc/hosts
# Hostname to remove.
HOSTLINE=^nas ^fas
removeline() {
if [ -n "$(grep -E "^$HOSTLINE" $ETC_HOSTS)" ]; then
echo "$HOSTLINE Found in your $ETC_HOSTS, Removing now...";
sudo sed -i '' "/^$HOSTLINE/d" $ETC_HOSTS
else
echo "$HOSTLINE was not found in your $ETC_HOSTS"
fi
}
Any suggestions would be appreciated.


I'm trying to remove hostnames beginning with ^nas and ^fas from /etc/hosts but for some reason, it's not working. I think in this script, I either added something or needs to be removed. Here is my script:
#!/bin/sh -x
ETC_HOSTS=/etc/hosts
# Hostname to remove.
HOSTLINE=^nas ^fas
removeline() {
if [ -n "$(grep -E "^$HOSTLINE" $ETC_HOSTS)" ]; then
echo "$HOSTLINE Found in your $ETC_HOSTS, Removing now...";
sudo sed -i '' "/^$HOSTLINE/d" $ETC_HOSTS
else
echo "$HOSTLINE was not found in your $ETC_HOSTS"
fi
}
Any suggestions would be appreciated.