LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Script and Loop error handling (https://www.linuxquestions.org/questions/programming-9/bash-script-and-loop-error-handling-325817/)

Kedelfor 05-21-2005 09:18 PM

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

rksprst 05-21-2005 10:57 PM

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



Kedelfor 05-22-2005 05:12 AM

Thank you. I will give that a try.

kv_kumaravel 05-22-2005 05:25 AM

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

eddiebaby1023 05-22-2005 02:07 PM

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.

rksprst 05-22-2005 02:22 PM

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.


All times are GMT -5. The time now is 10:56 AM.