Break command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Break command
# 1  
Old 01-23-2009
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  
Old 01-23-2009
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  
Old 01-24-2009
Quote:
Originally Posted by bakunin
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  
Old 01-24-2009
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:

Code:
ping -c 1 host

And after that, read the return value ($?) if You need to take action if it fails or is successful. Another example:

Code:
lakris@landet:~/projekt$ ping -c 1 localhost; echo $?
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.039 ms

--- localhost ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.039/0.039/0.039/0.000 ms
0
lakris@landet:~/projekt$ ping -c 1 unknownhost; echo $?
ping: unknown host unknownhost
2
lakris@landet:~/projekt$

Regards,
/Lakris
# 5  
Old 01-24-2009
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
# 6  
Old 02-10-2009
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 adviceSmilie



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
# 7  
Old 02-10-2009
Quote:
Originally Posted by venkidhadha
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 Smilie

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Break in for loop

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)
Discussion started by: ctrld
1 Replies

2. Solaris

Can't use 'break' command, Can't access 'ok' prompt.

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)
Discussion started by: SomeoneTwo
3 Replies

3. Shell Programming and Scripting

How to break the line to the one above?

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)
Discussion started by: demmel
4 Replies

4. Shell Programming and Scripting

BASH: Break line, read, break again, read again...

...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)
Discussion started by: SilversleevesX
2 Replies

5. Shell Programming and Scripting

break: cannot break

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)
Discussion started by: karlochacon
7 Replies

6. Shell Programming and Scripting

how to break cat

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)
Discussion started by: buddhist
2 Replies

7. Solaris

Break command on rsc of sunfire v890 server

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)
Discussion started by: girish.batra
6 Replies

8. UNIX for Dummies Questions & Answers

Can anyone break down this find command for me?

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)
Discussion started by: knp808
1 Replies

9. Shell Programming and Scripting

break out of 'if'

is it possible? because i still need to keep on reading even though i don't want to read that particular line (7 Replies)
Discussion started by: finalight
7 Replies

10. UNIX for Dummies Questions & Answers

Insert line break in vi's command mode

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)
Discussion started by: Skogsmulle
3 Replies
Login or Register to Ask a Question