Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie help: grep/find script

Status
Not open for further replies.

starla0316

Programmer
Sep 8, 2003
15
SG
Hello,

I would like to know if anyone has a suggestion on how I can make a script to grep/find a file in a certain directory. If the file is not yet there, it will wait for 5 minutes. If it is in the directory already, the script will then proceed to the next steps.

Thank you
 
Code:
while : # loop forever
do
  [[ -f FileToFind ]] && break # exit if the file is there
  sleep 300 # sleep for 300 seconds - five mins
done

or, sligtly easier to read
Code:
while : # loop forever
do
  if [ -f FileToFind ]
  then
    break
  fi
  sleep 300 # sleep for 300 seconds - five mins
done
but the logic is the same

Ceci n'est pas une signature
Columb Healy
 
thank you, is there anything to be added if it has already been 20 minutes and the file is not there, so it should already exit out of the script and create an error message?
 
If you want it only to wait for 20 mins max then you need a counter i.e.
Code:
#!/bin/ksh

COUNT=0
MAX_COUNT=4 # 4 times five mins is 20
SLEEP_TIME=300 # put this as a parameter so it's easy to maintain

while [ $COUNT -lt $MAX_COUNT ]
do
  [[ -f FileToFind ]] && break # If the file exists exit loop
  (( $COUNT += 1 )) # Add 1 to count each time round
done

Ceci n'est pas une signature
Columb Healy
 
Oops - missed out the 'sleep'!!!
The code should be
Code:
#!/bin/ksh

COUNT=0
MAX_COUNT=4 # 4 times five mins is 20
SLEEP_TIME=300 # put this as a parameter so it's easy to maintain

while [ $COUNT -lt $MAX_COUNT ]
do
  [[ -f FileToFind ]] && break # If the file exists exit loop
  (( $COUNT += 1 )) # Add 1 to count each time round
  sleep $SLEEP_TIME
done

Ceci n'est pas une signature
Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top