Article 4Z5Y3 Removing hostnames from /etc/hosts with Bash

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.latest?d=yIl2AUoC8zA latest?i=T29RXyWDZO4:NPNWu6099m4:F7zBnMy latest?i=T29RXyWDZO4:NPNWu6099m4:V_sGLiP latest?d=qj6IDK7rITs latest?i=T29RXyWDZO4:NPNWu6099m4:gIN9vFwT29RXyWDZO4
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments