Strange exit of while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strange exit of while loop
# 1  
Old 07-07-2011
Strange exit of while loop

This code is used to check for duplicate ip and hostnames in an /etc/hosts file

CENTRAL is path to /etc/hosts
AWK =awk

Code:
 #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
Code:
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 Smilie 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..
Code:
ret=`grep -v "^#" $CENTRAL  | grep -w "$ip"  | wc -l`
ret1=`grep -v "^#" $CENTRAL  | grep -w "$hostname" | wc -l`

# 2  
Old 07-07-2011
how about doing it all inside of awk...
Code:
awk '$0 !~ "^#" {
  ip[$1]++
  h[$2]++
} END {
  for(i in ip) if(ip[i]>1) print "dup ips "i
  for (j in h) if (h[j]>1) print "dup host "j
}' /etc/hosts

This User Gave Thanks to shamrock For This Post:
# 3  
Old 07-07-2011
Do you need doing it through awk would make it faster? The files this will be in production with will have vary large files to go through
# 4  
Old 07-07-2011
Quote:
Originally Posted by trimike
Do you need doing it through awk would make it faster? The files this will be in production with will have vary large files to go through
Dont how by how much precisely but it will be faster as your shellcode uses a lot more programs grep awk etc. to do the same thing which one instance of awk can do...mainly because forking processes is an expensive operation. How big do you expect your /etc/hosts file to be...dont you use DNS at your site.
# 5  
Old 07-08-2011
There will be around 1500-2000 lines. Yeah but DNS is run by the Windows team......heaven forbid there be a zone transfer to a UNIX/Linux box Smilie

---------- Post updated at 02:32 PM ---------- Previous update was at 01:14 PM ----------

hey shamrock if i wanted to use your code from above and exclude blank lines as well as lines with # at the beginning how would i add that functionality?

---------- Post updated 07-08-11 at 08:03 AM ---------- Previous update was 07-07-11 at 02:32 PM ----------

For all those interested I ended up going with a code that looks like this:

I added the egrep to filter out any blank or commented lines. This way is so much faster than my original post. Thanks shamrock.

Code:
#check CENTRAL for duplicate ips or hostnames#
        centcheck=`egrep -v '^#|^$' $CENTRAL | $AWK -v me=$ME '{
                ip[$1]++
                h[$2]++
                } END {
                for(i in ip) if(ip[i]>1) print me ":Duplicate IP in hosts.central " i
                for(j in h) if(h[j]>1) print me ":Duplicate hostname in hosts.central " j
                }'`
        if [ "$centcheck" != "" ]
        then
                echo "$centcheck"
                error=true
        fi

# 6  
Old 07-08-2011
Quote:
Originally Posted by trimike
hey shamrock if i wanted to use your code from above and exclude blank lines as well as lines with # at the beginning how would i add that functionality?
Throw away the egrep portion of your code and use the one below...
Code:
awk '$0 !~ "(^#|^$)" {
  ip[$1]++
  h[$2]++
} END {
  for(i in ip) if(ip[i]>1) print "dup ips "i
  for (j in h) if (h[j]>1) print "dup host "j
}' /etc/hosts

$0 !~ "(^#|^$)" tells awk to discard blank lines ^$ and those that begin with a comment ^#.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using exit in For Loop - Is this acceptable

Hi Folks - Here is a for loop I've created and I just wanted to see if this was okay practice: for M in NAME1 NAME1 NAME3 do echo "Executing MaxL:" $M >>${_LOGFILE} 2>&1 . ${_STARTMAXLPATH}startmaxl.sh ${_MAINPATH}${_MAXLPATH}$M.mxl _RC=$? if then ... (7 Replies)
Discussion started by: SIMMS7400
7 Replies

2. Emergency UNIX and Linux Support

For loop exit

Below for loop not exiting. Can someone help? JBOSS_INST_ARGS=01 02 if ; then for i in $JBOSS_INST_ARGS; do /u/jboss-6.1.0.Final/bin/jboss_init_wise$i.sh start; done (8 Replies)
Discussion started by: vino_hymi
8 Replies

3. Shell Programming and Scripting

Loop exit control

Hi I would like to exit the loop below on <Enter> even if it sleeps. Is it possible? while true do my_procedure; sleep 60 done Thanks (7 Replies)
Discussion started by: zam
7 Replies

4. Shell Programming and Scripting

Exit from loop

hi, how to exit from "if" loop?actually i have mutliple "if" conditions, i have to exit from each "if" loop,if it is true...:confused: Please suggest me... (3 Replies)
Discussion started by: sreelu
3 Replies

5. Programming

The C for() loop - A strange observation

Hello, I am optimizing my low level C coding style. I have run into an strange C implementation fact: ---------------------------------------------------- unsigned int X; for ( X = 0; X < 10; X++ ) printf("%u\n",X); Produces: 0 1 2 3 4 5 6 7 8 (3 Replies)
Discussion started by: HeavyJ
3 Replies

6. Shell Programming and Scripting

Exit from loop only when selected

I am trying to get my program to exit when the answer to my question is positive, if I am asking if the answers are correct in the entries that the user inputted and the user says no how do I then have it exit? If they say everything is correct then it continue into the program, I think I am close... (2 Replies)
Discussion started by: gumbi17
2 Replies

7. Shell Programming and Scripting

exit out of a while loop with error

im running a while loop as a file watcher, with incremental counter on the retries..however when the retries reach it's limit i want it exit and echo and error and stop the batch. Im not sure the code i have will do that already... Here is what i have that works: #!/usr/bin/ksh count=0... (2 Replies)
Discussion started by: sigh2010
2 Replies

8. Shell Programming and Scripting

how to exit a while true loop

Hi guys, I'm new to unix but loving it!! BUT this is driving me nuts as i can't work out the best way to do it. I have a while true loop that i use to monitor something. For my own reasons in ths script i have disabled the CTRL C using the trap command. But i want to put in a option to exit... (5 Replies)
Discussion started by: Noob e
5 Replies

9. Shell Programming and Scripting

Method to exit a for loop

Hi All, Can someone let me know how i can exit a for loop without exiting the script itself .... will the break statement work .... please help .... -Regards (2 Replies)
Discussion started by: Rohini Vijay
2 Replies

10. Shell Programming and Scripting

while loop exit

i wrote a while script as part of a huge program. this script, once picked, begins to output data to the person using it. pretty easy, as the person doesn't have to keep typing commands to get the output that the while loop automatically throws out. now, the thing is, while this while-script... (3 Replies)
Discussion started by: Terrible
3 Replies
Login or Register to Ask a Question