Operating System - Linux
1755676 Members
3054 Online
108837 Solutions
New Discussion юеВ

Help regarding while loop.

 
SOLVED
Go to solution
Raghu Chikkamenahalli
Frequent Advisor

Help regarding while loop.

Hello All,

Looking for a pointer for the following.
Script should Wait for a response file ( some text file) for 100mins, if the file comes with in 100min, script should continue.
If the file doesn't arrive after 100min, it needs to abort the script.
Can you please through some pointer on this.

Regards,
Raghu.
3 REPLIES 3
MarkSyder
Honored Contributor

Re: Help regarding while loop.

I'm going to assume you're writing a shell script rather than (for example) a C program.

while :
do
if
[ -f $your_file ]
then
do next bit of script
else
let counter=counter+1
if
[ $counter == 10 ]
then
break
else
sleep 600
fi
fi

This tests for the existence of the file every 10 minutes. If the counter reaches 10, 100 minutes have elapsed and the script breaks if the file does not exists. If you want to check more often, reduce 600 and increase counter.

I haven't tested this - it's "back of an envelope" stuff - so it may need a tweak or two, but I think it will set you on the way.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
James R. Ferguson
Acclaimed Contributor
Solution

Re: Help regarding while loop.

Hi Raghu:

Here's another shell variation. Change the values of MAXSECS, STEP and FILE to meet your needs.

# cat ./watch
#!/usr/bin/sh
typeset -i MAXSECS=15
typeset -i STEP=3
typeset FILE=/tmp/mything
while true
do
if [ -f "${FILE}" ]; then
echo "'${FILE}' is now available"
break
fi
MAXSECS=${MAXSECS}-${STEP};
if (( ${MAXSECS} < 0 )); then
echo "No file seen"
exit 1
fi
sleep ${STEP}
done
exit 0

Regards!

...JRF...
Raghu Chikkamenahalli
Frequent Advisor

Re: Help regarding while loop.

Hi JRF,

Awesome script. Great help indeed.

Regards.
Raghu