This code is used to check for duplicate ip and hostnames in an /etc/hosts file
CENTRAL is path to /etc/hosts
AWK =awk
#check CENTRAL for duplicate ips or hostnames#
grep -v "^#" $CENTRAL | $AWK '{ print $1, $2; }' | \
while read ip hostname
do
if [ "$ip" != "" -a "$hostname" != "" ]
then
echo $ip $hostname
ret=`grep -v "^#" | grep -w "$ip" $CENTRAL | wc -l`
ret1=`grep -v "^#" | grep -w "$hostname" $CENTRAL | wc -l`
if [[ $ret > 1 ]]
then
echo "$ME:Duplicate IP in hosts.central: $ip $hostname"
error=true
elif [[ $ret1 > 1 ]]
then
echo "$ME:Duplicate hostname in hosts.central:$ip $hostname"
error=true
fi
fi
done
The result from this code is that it only iterates through 1 line
output:
127.0.0.1 localhost
strange enough when I comment out
ret=`grep -v "^#" | grep -w "$ip" $CENTRAL | wc -l`
ret1=`grep -v "^#" | grep -w "$hostname" $CENTRAL | wc -l`
it gives me my intended ouput
127.0.0.1 localhost
192.168.2.1 myip
192.168.2.3 myip
Note the $CENTRAL file contains the following entries
127.0.0.1 localhost
192.168.2.1 myip
192.168.2.3 myip
Any thoughts :wall: Thanks
---------- Post updated at 11:07 AM ---------- Previous update was at 10:55 AM ----------
I believe I just found my issue..
my grep form is off..
ret=`grep -v "^#" $CENTRAL | grep -w "$ip" | wc -l`
ret1=`grep -v "^#" $CENTRAL | grep -w "$hostname" | wc -l`