help in handling loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help in handling loops
# 1  
Old 08-01-2004
Question help in handling loops

Hi,

I am using bash script

I want some help in handling loops in my script...

I need to check for a specific interval of time whether a file in a paritcular directory exists. If exists, i need to do some action and continue with the next iteration..

//my script

attempt=0
totalattempts=2

until [ $attempt -gt `expr $totalattempts - 1`]
do
i=0
until [ $i -gt ${#input[@]} -1 ]
do

if [ -f ${input[i]}/out.txt ]; then
//blah blah blah

continue

else
sleep 60
fi
done
done


Say my input array contains test1, test2, test3,test4...etc

Now i have to loop to check for the out.txt in the ${input[i]} directory.If out.txt is found in a particular directory, i shuould not check that directory the next time.

For example If test2/out.txt is found in my first attempt, i should be continuing with checking only test1/out.txt, test3/out.txt for the next consecutive attempts.
I should avoid checking test1/out.txt.

I am not sure how to handle that....Can anyone please help?

Thanks in adv
# 2  
Old 08-02-2004
It's a bit kludgey, but you could always append the value to a temporary file if it exists, and check that next time round

e.g. your code segment

Code:
if [ -f ${input[i]}/out.txt ]; then
//blah blah blah

continue

else
sleep 60
fi

could be changed to

Code:
if [ -f ${input[i]}/out.txt ]; then

   grep "${input[i]}" tempfile >/dev/null 2>&1

   if [ "$?" -ne 0 ]; then  # it isn't in tempfile, so
      # append the filename to the tempfile so its avoided
      # next time around
      echo "${input[i]}" >> tempfile
      # do processing here so it's only processed the first
      # time its encountered
   fi

   continue

else
   sleep 60
fi

Remember to rm the tempfile when it's no longer needed, and set up a trap, etc.

Just an idea

Cheers
ZB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help with for loops

Why wont my for statements work? Im trying to get this script to swich to a user an if you put in a start/stop/or restart paramater to do just that for each user. I commented out the actual start/stop actions to test it just by using echos and not do anything hasty in the environment but it... (0 Replies)
Discussion started by: LilyClaro
0 Replies

2. UNIX for Advanced & Expert Users

Help with loops?

I'm trying to understand better the while and until loops, can someone help me with this example? #!/bin/bash # Listing the planets. for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto do echo $planet # Each planet on a separate line. done echo; echo for... (3 Replies)
Discussion started by: jose2802
3 Replies

3. Shell Programming and Scripting

Too many for loops

My apologies for the long post.... I have a structure problem with my ksh script on linux. right now it does what i want (which i know is the point) but its really really ugly and i know there must be a simpler way to achieve my task. The data is a list of usernames. This list will help... (2 Replies)
Discussion started by: maverick72
2 Replies

4. Shell Programming and Scripting

loops

Hi All I have some directories on our server which are containing .csv files. i need to print value of cell "B2" from those csv files. Please advise. I have tried head command as example: head -2 */Book_Collection_Report_1_-_Collection_Requests_trials.csv | sed -n "3p" | awk -F","... (4 Replies)
Discussion started by: yash1978
4 Replies

5. Shell Programming and Scripting

Handling directory with spaces in for loops

Hi everyone, I have been a big fan here since a couple years (since I started being an admin ...) and finally decided to become a member and help ppl and perhaps being helped Now I have a problem that might interest some of the gurus. I am abig fan of what I call "one liners". I am trying... (2 Replies)
Discussion started by: plmachiavel
2 Replies

6. Shell Programming and Scripting

Help with loops

I am trying to do ftp of some crt files that have spaces in the file names. I have to do approximately 4500 files ftp'ed and then check whether the trasnfered file size matches the source files. This has to be in a loop I mean 1. ftp the file, check for file size whether the source and target... (0 Replies)
Discussion started by: dsravan
0 Replies

7. UNIX for Dummies Questions & Answers

two loops

Hi, how can I use "for" to have two loops : this is my script : for i in (A B C) do for j in (a b c) do echo $i$j done done #End I want to print out Aa Ab Ac .... But I have error message : syntax error at line 1 : `(' unexpected Many thanks before. How should I use "for" ?? (2 Replies)
Discussion started by: big123456
2 Replies

8. UNIX for Dummies Questions & Answers

While Loops

I'm trying to create a loop that will prompt the user for 15 values, not forcing them to enter all 15. If the user enters through one or more of the prompts the null value needs to be converted to 0, otherwise set the parameter = to the value entered: ex. Please enter file no #1: 17920 ... (4 Replies)
Discussion started by: vdc
4 Replies

9. Shell Programming and Scripting

Loops within loops

I am running on HPUX using ksh. I have a script that uses a loop within a loop, for some reason the script seems to hang on a particuliar record. The record is fine and hits the condition in Blue. If I kill the 1st loop process the script continues on with no problem. Begin code> <Some... (8 Replies)
Discussion started by: bthomas
8 Replies

10. UNIX for Advanced & Expert Users

Loops

Can anybody help please. I am trying to right a script which will loop until a certain action has been performed. For example i current have two batch jobs i would like to put into a wait status. Batch Jobs A and B . The script i am trying to get to work is below. jobs="A B" COUNT=0 while... (2 Replies)
Discussion started by: mariner
2 Replies
Login or Register to Ask a Question