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.
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.
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.
Hello there,
I think You should consider using ping with the count option, otherwise it will continue pinging forever and You will never get out of the loop. Example:
And after that, read the return value ($?) if You need to take action if it fails or is successful. Another example:
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.
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
Last edited by bakunin; 02-12-2009 at 04:39 AM..
Reason: added "code"-tags, reindented the 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
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:
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:
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:
which means, "if the ping command isn't successful ( || is an "else" ), then send me a mail about it". Try && for a different outcome...
in my python script i have loop like below:
for item in itemlist:
if <condition>:
<code>
else:
<code>
if <condition>:
if <condition>:
<code>
else:
for type in types:
if... (1 Reply)
Hi
I have A Sun Ultra Enterprise 450 server, it has Solaris installed on it.
I have A serial terminal hooked up to it (nullmodem cable plugged into serial port 1 on the box, and the other end plugged into the serial port of A laptop (NEC Versa M300))
The laptop is running Ubuntu 12.04.2... (3 Replies)
Hello everyone!
I'm trying to make the below file1 look like file2, can anyone help?
Basically I just hit backspace on every line that starts with a number.
Thanks!
file1:
THIS#IS-IT1
4
THIS#IS-IT2
3
THIS#IS-IT3
2
THIS#IS-IT4
1
Result > file2: (4 Replies)
...when the lines use both a colon and commas to separate the parts you want read as information.
The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
hi guys
I am working on a menu for linux... some basic stuff.
but I have an issue. I got 1 server where something is working and the same thing does not work in the same way in another linux box
Basically I am simulating a command line where user insert some commands and to end and go back... (7 Replies)
Greetings.
cat $name$telephonenumber >> telephonebook.txtI would like to break cat with the command 'break'. Pretty hard to understand huh? So to clarify it:
echo "If you want to stop adding datas to your telephonebook please type 'break'
if #this part is probably not good
then
echo... (2 Replies)
Hi Gurus
I would like to know whether break command always work well means take the system to ok> prompt. Its depend upon sever model also. As I am facing problem when ever my system ( Sunfire V890 ) having Solaris 10 OS & oracle RAC on it goes to hang state then I run the break command from... (6 Replies)
find . "(" -name a.out -o -name core ")" -exec rm {} \;
Specifically
What files are trying to be found
What does the -o do in this command
What is the result if the files are found
What does the command do if the files are not found
What does the . after the word find mean
thanks in... (1 Reply)
Hi,
When working in vi, the CTRL+j command for merging lines is very convenient. Is there an equivalent for splitting them (inserting a line break)? I often find myself pressing "i" + "return" + "esc", which I find a bit lengthy.
Thanks in advance! (3 Replies)