Sponsored Content
Full Discussion: Strange exit of while loop
Top Forums Shell Programming and Scripting Strange exit of while loop Post 302537222 by trimike on Thursday 7th of July 2011 12:07:44 PM
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`

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 07:07 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy