Hello All,
I am learning BASH scripting and I would appreciate any help with a small problem I am having...
I am writing a script that builds a simple hosts file for DNS reasons related to a piece of software called netdb by parsing another application's config files for IP's and their hostnames.
The script works correctly for all hosts except a relatively small group. What ends up happening is the string my script finds for a hostname ends up overwriting everything on the line that it is currently located on. I've narrowed my script down to only parse the config file that produces these problem strings after my script runs.
The erroneous output is as follows:
Number of addresses found is: 2
Number of DNS names found is: 2
Sync Check Success!
-----------------------------------------------------------
DEBUG: VARIABLE (IP) IS: 192.168.xxx.xxx
DEBUG: VARIABLE (host) IS: Alderaan
-----------------------------------------------------------
DEBUG: VARIABLE (IP) IS: 192.168.xxx.xxx
DEBUG: VARIABLE (host) IS: Bespin
Bespin .xxx.xxx
Alderaan .xxx.xxx
Number of addresses successfully converted is: 2
When the output should be:
Number of addresses found is: 2
Number of DNS names found is: 2
Sync Check Success!
-----------------------------------------------------------
DEBUG: VARIABLE (IP) IS: 192.168.xxx.xxx
DEBUG: VARIABLE (host) IS: Alderaan
-----------------------------------------------------------
DEBUG: VARIABLE (IP) IS: 192.168.xxx.xxx
DEBUG: VARIABLE (host) IS: Bespin
Bespin 192.168.xxx.xxx
Alderaan 192.168.xxx.xxx
Number of addresses successfully converted is: 2
here is my script code
#!/bin/sh
export IFS="
"
TotalAdrFound=`grep -E "address" -h xxx.cfg |cut -d ";" -f1|wc -l`
TotalDNSFound=`grep "A longer name" -h xxx.cfg|wc -l`
#Use grep to parase .cfg file and grab the ip number for each host
args=`grep "address" -h xxx.cfg| awk '{print $2;}'`
#Remove old list of DNS names.grep "address" -h *.cfg| cut -d "s" -f3 | cut -d ";" -f1 |
rm /tmp/addrs.netdb
#Use grep to parase .cfg file and grab the host name and output to addrs.netdb
sudo grep "A longer name" -h xxx.cfg| cut -d ";" -f1 |awk '{print $2;}' >> /tmp/addrs.netdb
TotalAdrSuccess=0
echo "Number of addresses found is: "$TotalAdrFound
echo "Number of DNS names found is: "$TotalDNSFound
#Check to see if every host has an IP address.
if [ "$TotalAdrFOUND"="$TotalDNSFound" ] ;
then
echo "Sync Check Success!"
else
echo "Sync Check FAILLED! There has been some type of error. The Total Number " \
echo " of Addresses found does not match the total number of DNS host names found."
fi
for IP in $args ;do
#Increment the line number for the sed command below
Line=$((Line+1))
#grab the host name for the IP
host=`sed "$Line!d" /tmp/addrs.netdb`
TotalAdrSuccess=$((TotalAdrSuccess+1))
#Some debug code for a select group of hosts.
echo "-----------------------------------------------------------\n"
echo "DEBUG: VARIABLE (IP) IS: " $IP
echo "DEBUG: VARIABLE (host) IS: " $host
output="$IP $host\n$output"
done
echo "$output"
echo "Number of addresses sucessfully converted is: "$TotalAdrSuccess
Like I said above this works with all my other cfg files but this one. This cfg file is in the same format as all the other.