LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2005, 10:18 PM   #1
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Rep: Reputation: 15
Bash Script and Loop error handling


OK I have another question on bash scripting.

I am writing a script that uses a file that has an array of Computer locations.

I use a for loop to go through each one.

My question is I test to see if there is communication to the server before moving on. Like I want to copy a file, but I want to test if I can communicate to the site first before attempting.

My question is if the communication test fails how would I break out of that current loop so it wouldn't move onto trying to get the files from the server, but go back to the beginning and move on to the next server in the list. I also want to generate an error in a log file that i am creating. I know how to handle the writing into the log, but I am afraid it might continue and try getting the file anyway.

Any help would be appreciated.

Thanks
 
Old 05-21-2005, 11:57 PM   #2
rksprst
Member
 
Registered: Jan 2004
Distribution: OS X 10.4
Posts: 172
Blog Entries: 1

Rep: Reputation: 30
you can simply use the break command

it would go something like this:
if (communication is not working)
then (write to the log file AND break)

Quote:
The break command may optionally take a parameter. A plain break terminates only the innermost loop in which it is embedded, but a break N breaks out of N levels of loop.

Code:
#!/bin/bash
# break-levels.sh: Breaking out of loops.

# "break N" breaks out of N level loops.

for outerloop in 1 2 3 4 5
do
  echo -n "Group $outerloop:   "

  for innerloop in 1 2 3 4 5
  do
    echo -n "$innerloop "

    if [ "$innerloop" -eq 3 ]
    then
      break  # Try   break 2   to see what happens.
             # ("Breaks" out of both inner and outer loops.)
    fi
  done

  echo
done  

echo

exit 0
 
Old 05-22-2005, 06:12 AM   #3
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Original Poster
Rep: Reputation: 15
Thank you. I will give that a try.
 
Old 05-22-2005, 06:25 AM   #4
kv_kumaravel
LQ Newbie
 
Registered: May 2005
Location: india
Posts: 2

Rep: Reputation: 0
shall i can give the 'N' level in run time
i mean the loop will rotates according to the givan value run time

Last edited by kv_kumaravel; 05-22-2005 at 06:41 AM.
 
Old 05-22-2005, 03:07 PM   #5
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
You do something like:
Code:
HOSTS="space separated list of machines to try"

for host in $HOSTS
do
    # test if communciation works
    if # test succeeded
    then
        # do the file copy and anything else required
    fi
done
You'll execute the loop for each name in the HOSTS variable, but only execute the file copying block if the communication test succeeded. I'd use something like ping(1) to check the target host is online.
 
Old 05-22-2005, 03:22 PM   #6
rksprst
Member
 
Registered: Jan 2004
Distribution: OS X 10.4
Posts: 172
Blog Entries: 1

Rep: Reputation: 30
Quote:
Originally posted by kv_kumaravel
shall i can give the 'N' level in run time
i mean the loop will rotates according to the givan value run time
in "break N", the N is simply how many nested loops you want to break out of... it has nothing to do with the actual amount "rotation" of the loop
Quote:
a break N breaks out of N levels of loop.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script loop bong.mau Programming 6 09-14-2005 08:38 PM
Handling whitespace in For loop rvoigt Linux - General 1 04-06-2005 07:57 AM
bash script for loop drisay Programming 5 12-25-2004 01:32 AM
bash script - for loop question rignes Programming 3 10-06-2004 12:16 AM
while loop ending prematurely in a bash script meat-head Programming 7 05-08-2004 02:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration