Operating System - HP-UX
1753779 Members
7654 Online
108799 Solutions
New Discussion юеВ

Check for file multiple times

 
SOLVED
Go to solution
John Crump
Occasional Contributor

Check for file multiple times

Please forgive this I think rather basic question.

I have been away from UNIX for a very long time and am in need of some help.

I need to be able to check for the existance of a specific file name say 'file.dat' in a particular location

If the file exists then run a second process (at processname now)

If the file does not exist then sleep 5 minutes and look for it again for up to 3 times

Once the file has been found and processed once only then finish

Any hints tips or specific examples would be appreciated

Running HPUX by the way

This is what I have so far

The process searches up to 3 times for the arival of a file. Once it has detected the file and processed the job , it is important that it does not do so again. How can I prevent this?

2 In this version of the code the script does not loop back to try again when the file is not found

#!/bin/ksh
file="/path/to/file/file.dat"


for i in 1 2 3
do
if [[ -f $file ]] ; then echo " file found"
at -f jtest_get_file.sh now
else
echo "File not found!".

break
fi
sleep 3 # 3 seconds will be 600
done



Regards

8 REPLIES 8
Steven E. Protter
Exalted Contributor

Re: Check for file multiple times

Shalom,

Suggest a while loop

while true
do


# your code

done

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Dennis Handly
Acclaimed Contributor

Re: Check for file multiple times

>Once it has detected the file and processed the job, it is important that it does not do so again.

Well, you can remove the file. You can rename/move the file. Or you can change the permission on the file.

Or you can create another file that says you processed the file. Of course you need to clean up two files in order to start this process again.

Or you can just exit the script?
John Crump
Occasional Contributor

Re: Check for file multiple times

Steven,

many thanks for your help so far

I now have


while true
do
for i in 1 2 3
do
if [[ -f $file ]] ; then echo " file found"
at -f jtest_get_file.sh now
extracted='y'
echo $extracted
else
echo "File not found!".
break
fi
sleep 3 # 3 seconds will be 600
done

Unfortunatly it is now looping for ever

By the way I see from your profile who you work for. I helped to impliment an Oracle based ERP system in your Southampton office in the UK

Regards
John Crump
Occasional Contributor

Re: Check for file multiple times

Dennis,

Thank you for your help.

I am happy with both of your sugestions. I am fine with moving or re naming the file , but how would I exit the script after the secdond process has run once only?

What is the syntax and where would I position it?

Regards
James R. Ferguson
Acclaimed Contributor

Re: Check for file multiple times

Hi John:

Try this variation:

#!/usr/bin/sh
look=0
while true
do
if [[ -f "${file}" ]] ; then
echo "'$file' file found"
at -f jtest_get_file.sh now
extracted='y'
echo $extracted
exit 0
fi
echo "File not found!"
(( look=$look+1 ))
[ $look -ge 3 ] && exit 1
sleep 3
done

Regards!

...JRF...
John Crump
Occasional Contributor

Re: Check for file multiple times

James,

Thank you sir !

That works perfectly

Many thanks for you help

Regards

John
James R. Ferguson
Acclaimed Contributor
Solution

Re: Check for file multiple times

Hi John:

You are welcome! ...please read too:

http://forums12.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Check for file multiple times

>I am happy with both of your suggestions. I am fine with moving or renaming the file, but how would I exit the script after the second process has run once only?
>What is the syntax and where would I position it?

You can use exit to get out of the script, return to get out of the function. Or you can use break to get out of the loop.
I assume this is where your "echo $extracted" is?
extracted='y'
echo $extracted
break 2

The "2" is needed to get out of both the "for" and "while" loops.

I'm not sure why you want that while there? I assumed Steven meant replace that "for" loop by a while loop? (It's hard to use "for" for lots of times.

#!/usr/bin/sh
file="???"
typeset -i count=3
extracted=
while (( (count -= 1) >= 0 )); do
    if [ -f "${file}" ]; then
       echo "'$file' file found"
       at -f jtest_get_file.sh now
       extracted='y'
       echo $extracted
       break
    fi
    echo "File not found!"
    (( count == 0 )) && break; # skip last sleep
    sleep $(( 5 * 60 )) # 5 minutes
done
# would need to test $extracted if desired