![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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 |
|
|||||
|
Quote:
Quote:
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
Quote:
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 Example: Code:
ping -c 1 $k 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 Best regards, Lakris |
|
||||
|
Quote:
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
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.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|