The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can anyone break down this find command for me? knp808 UNIX for Dummies Questions & Answers 1 12-09-2008 11:07 PM
break out of 'if' finalight Shell Programming and Scripting 7 11-19-2008 03:54 PM
Page Break with AWK or SED udaybo Shell Programming and Scripting 4 06-18-2008 11:03 AM
Break mirror so that it can be used later AQG HP-UX 2 10-10-2007 09:03 PM
Insert line break in vi's command mode Skogsmulle UNIX for Dummies Questions & Answers 3 07-06-2007 10:47 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-23-2009
venkidhadha venkidhadha is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
Break command

have a query on this break. I am using a program where I am using a while loop to execute it. It will get into a file take the first file and then ping it and if the ip is reachable then it will mail. Now what happens is that when i ping the ip it does nopt come out of the loop and it says "reply from reply from" . I need to usse a break statemetn to come out of this. Can you assist me in this.

Let me know if i need to explain more.
  #2 (permalink)  
Old 01-23-2009
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
To really help you with your problem it would be best to post your code - otherwise we are just left to guessing what the error might be.

Generally speaking it is better to not use "break" at all: it is basically a "GOTO" (in fact it is a "GOTO END OF LOOP", which is a hallmark of unstructured programming. You might want to read the famous article A Case against the GO TO statement by Edsger W. Dijkstra to better understand why this is bad practice.

The same is true, btw., for the "continue" statement.

I hope this helps.

bakunin
  #3 (permalink)  
Old 01-24-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by bakunin View Post
To really help you with your problem it would be best to post your code - otherwise we are just left to guessing what the error might be.

Absolutely!
Quote:
Generally speaking it is better to not use "break" at all: it is basically a "GOTO" (in fact it is a "GOTO END OF LOOP", which is a hallmark of unstructured programming. You might want to read the famous article A Case against the GO TO statement by Edsger W. Dijkstra to better understand why this is bad practice.

The same is true, btw., for the "continue" statement.

Nonsense!
  #4 (permalink)  
Old 01-24-2009
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 243
And by the way,
why the hangup about GOTO?
It is of course up to the programmer to construct legible and functional code, and for a small program or asm it is very useful. AND can be very readable too. Of course it is silly to use GOTO in a large program where it's hard to keep track of the state of things. But everything's not black and white.

/Lakris
  #5 (permalink)  
Old 02-10-2009
venkidhadha venkidhadha is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 19
Here is the code snippet which i am using . actually wheen it tries to ping a node it pings it and then ig et a message sayng " Time exceeded" then it doesent come outside of the loop, which triggers more than 50 mails in 5 secs. Please advice



Code:
i=1
while [ $i -le 7 ]
do
     k=`cat iplist |head -$i|tail -1`
     ping $k
     if [$? -eq 0]
          then echo " The node $k is alive " |  mailx -s " Network connectivity " venki_dadad @in.com
          else echo " The node $k is down  " | mailx -s " Network connectivity " venki_dadad @in.com
          i=`expr $i + 1`
          echo $i
     fi
done

Last edited by bakunin; 02-12-2009 at 04:39 AM.. Reason: added "code"-tags, reindented the code
  #6 (permalink)  
Old 02-10-2009
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 243
Quote:
Originally Posted by venkidhadha View Post
i=1
while [ $i -le 7 ]
do
k=`cat iplist |head -$i|tail -1`
ping $k
if [$? -eq 0]
then echo " The node $k is alive " | mailx -s " Network connectivity " venki_dadad @in.com
else echo " The node $k is down " | mailx -s " Network connectivity " venki_dadad @in.com
i=`expr $i + 1`
echo $i
fi
done
Hi, did You read my previous post about ping?
I can think of a few problems with Your code, but let's be systematic

1) If You have a list of hosts in a file, if iplist is a list of k's, then use that as a basis for Your loop and don't construct some strange mechanism that force You to change Your code just because the list changes.

Example:

Code:
while read k ; do
... some action here based on value of k ...
done < iplist
2) If You are on a *nix system, ping by default will continue to ping until You tell it to stop, so You must give it a condition so it's behaviour is predictable. As I said before. That IS Your break.

Example:

Code:
ping -c 1 $k
3) You can't have a space in the e-mail address, is it a typo? Make a habit of copy/paste when You submit code examples, otherwise it's very easy to make spleling mistakes.

And if You really want to know the status of the nodes, what is most important about it? To know if it's down? Maybe it's not very interesting to know that things are working as they should? Because, well, they should. Then just send a mail when a node is unreachable.

And if it's really important, why not put that information in the header, so it stands out?

Example:

Code:
while read k ; do
ping -c 1 $k &> /dev/null || echo "$k is DOWN, I repeat, $k is DOWN" |mailx -s "$k is DOWN" me@host 
done < iplist
which means, "if the ping command isn't successful ( || is an "else" ), then send me a mail about it". Try && for a different outcome...

Best regards,
Lakris
  #7 (permalink)  
Old 02-10-2009
ddreggors ddreggors is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 91
Quote:
Originally Posted by venkidhadha View Post
i=1
while [ $i -le 7 ]
do
k=`cat iplist |head -$i|tail -1`
ping $k
if [$? -eq 0]
then echo " The node $k is alive " | mailx -s " Network connectivity " venki_dadad @in.com

else echo " The node $k is down " | mailx -s " Network connectivity " venki_dadad @in.com
i=`expr $i + 1`
echo $i
fi
done
Assuming you only want the first 7 IP's from this file, try this also:
Code:
ips=`head -n 7 iplist`
for myip in $ips
do
     ping -c 1 $myip
     if [$? -eq 0]
     then 
         echo " The node $myip is alive " |  mailx -s " Network connectivity " venki_dadad @in.com 
     else 
         echo " The node $myip is down  " | mailx -s " Network connectivity " venki_dadad @in.com 
      fi
done
Or you could batch this in one shot like this:

Code:
ips=`head -n 7 iplist`
RESULT=""
for myip in $ips
do
     # Make this post to the console (remove the '2>&1 >> /dev/null' at the end)  or leave it so that it is quiet
     ping -c 1 $myip 2>&1 >> /dev/null
     if [$? -eq 0]
     then 
         RESULT="${RESULT} \nThe node $myip is alive "  
     else 
         RESULT="${RESULT} \nThe node $myip is down  " 
      fi
done
echo -e "${RESULT}" |  mailx -s " Network connectivity " venki_dadad @in.com

One email sent with all test results.

Last edited by ddreggors; 02-10-2009 at 10:54 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0