1823143 Members
3575 Online
109647 Solutions
New Discussion юеВ

Need scripting help

 
SOLVED
Go to solution
Paul Thomson_2
Super Advisor

Need scripting help

Hi

This a script was put out by one of my colleagues (honest) which did a sed and replaced an old hostname with a new host name for a server. However, it had to do this 9 times. This was done in individuial loops and the results >> to a new host file. The problem occured now in that we have 9 entries for each IP in /etc/hosts.

I have written a simple script to perform a check against each entry and then out put it to a new host file, however, I have found in some circumetances particualr lines on /etc/hosts are not picked up with a grep.

My script is attached. Any ideas how Icabn make sure it picks up each line when i perform `cat file | while read line` ??
Any ideas !?




Argh ye land lovers !
11 REPLIES 11
H.Merijn Brand (procura
Honored Contributor

Re: Need scripting help

Nothing is attached.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
jpcast_real
Regular Advisor

Re: Need scripting help

you can use awk '/xxxxx/' instead of grep . It is quite useful
Here rests one who was not what he wanted and didn't want what he was
Paul Thomson_2
Super Advisor

Re: Need scripting help

Will try attachment now !
Argh ye land lovers !
Prashant Zanwar_4
Respected Contributor

Re: Need scripting help

do sh -x <scriptname> and follow each line.
this is sort of debugging, but this will help you in knowing each line output.

Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."
Muthukumar_5
Honored Contributor

Re: Need scripting help

We can debug script with set -x to find problem easily,

-- your script ---
#!/usr/bin/sh
## Debugging
## set -x
LOG=/var/host.log
DUFF=/usr8/hosts.duff
GOOD=/usr8/hosts.good
cp /etc/hosts $DUFF
touch $GOOD
cat $DUFF | while read line
do
sleep 1
if
[ `grep "$line" $DUFF | wc -l` -gt 1 ] && [ `grep "$line" $GOOD | wc -l
` = 0 ]
then
echo "Duplicate entry in host file" >> $LOG
echo "$line" >> $GOOD
else
echo "Found the entry in the good host file, loop has completed." >> $LOG
fi
done
cp $GOOD /etc/hosts
chmod 444 /etc/hosts

Your are using ir-required logic's here. Good file will not be containing any entries why you are checkign there with duff file there??


There is no action done to change the hostname there in your script.

To change your hostname use sed / awk / perl program there.

IF you want to get duplication on /etc/hosts file then,

cp /etc/hosts /tmp/hosts

grep -vf /etc/hosts /tmp/hosts
will give duplication entries there.
Easy to suggest when don't know about the problem!
Jean-Luc Oudart
Honored Contributor

Re: Need scripting help

`grep "$line" $GOOD | wc -l
` = 0 ]
may read "`grep "$line" $GOOD | wc -l
` -eq 0 ]

Regards
Jean-Luc
fiat lux
Paul Thomson_2
Super Advisor

Re: Need scripting help

Muthukumar

My check for good file, is when it finds a line the next time it finds a duplicate entry the line will be in the good file so it will not echo $line again


Argh ye land lovers !
Paul Thomson_2
Super Advisor

Re: Need scripting help

Hi

Sorry, Im quite happy with the content of the script,as it does what it is supposed to.

But some entries in hosts are not picked up by grep "$line" /etc/hosts and I wondered why ? OR if anyone knew a better method. I have tried awk too on the same line and it cant find it either....

Thanks

Argh ye land lovers !
Bob Smith_23
Advisor

Re: Need scripting help

sort input-file | uniq > output-file
Muthukumar_5
Honored Contributor

Re: Need scripting help

I have used your script and tried your requirement so that,

the problem is because of if loop check there as,

if
[ `grep "$line" $DUFF | wc -l` -gt 1 ] && [ `grep "$line" $GOOD | wc -l
` = 0 ]

while checking number then use -eq or -ne etc there as,

if
[ `grep "$line" $DUFF | wc -l` -gt 1 ] && [ `grep "$line" $GOOD | wc -l
` -eq 0 ]

but any way it will be checking that,

1> line contains in the file more than once or line must not be there in good file.

so that it will add the contents to good file there.

your script is not checking duplicated liens which they located two lines later, etc there.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor
Solution

Re: Need scripting help

I got your problem that you are not copying the lines /etc/hosts files with count 1 to temporary file so that it is making problem there.

Try this script,

#!/usr/bin/sh
# Debugging mode
set -x

#### Log files ######
LOG=/var/host.log
DUFF=/usr8/hosts.duff
GOOD=/usr8/hosts.good

# hosts files copy #
cp /etc/hosts $DUFF
cp /etc/hosts /etc/hosts.def

# Create a new file if exits delete and create
[[ -f $GOOD ]] && rm -f $GOOD
touch $GOOD

# Duplication check
cat $DUFF | while read line
do

if [[ `grep "$line" $DUFF | wc -l` -gt 1 && `grep "$line" $GOOD | wc -l` -eq 0 ]]
then
echo "Duplicate entry $line in host file" >> $LOG
echo "$line" >> $GOOD
elif [[ `grep "$line" $DUFF | wc -l` -eq 1 && `grep "$line" $GOOD | wc -l` -eq 0 ]]
then
echo "$line" >> $GOOD
else
echo "Found the entry in the good host file, loop has completed." >> $LOG
fi
done

# Copy to hosts file
cp $GOOD /etc/hosts
chmod 444 /etc/hosts

It will work now.

Note: While your are operating on system files as like /etc/hosts without proper check don't operate there as,
cp $GOOD /etc/hosts
chmod 444 /etc/hosts

it will make that $GOOD files to be copied to /etc/hosts so that no entries will be there in /etc/hosts file too.


In My script I am using as,
cp /etc/hosts /etc/hosts.def
so that default will be there in /etc/hosts.def to get it for ever in future :-)

HTH.
Easy to suggest when don't know about the problem!