View Full Version : How to break out of a WHILE loop after a certain amount of time?

November 23rd, 2009, 06:59 AM
I have a script that checks many subdirectories looking for "foo.txt".

I'm an utter novice, so I'm probably going about it all wrong.

we have:


When foo.txt is found in one of those subdirectories, the subdirectory is moved. Eventually, foo.txt will exist in all subdirectories and /working/path will become empty, but it might take 10 seconds, or 2 hours. My script works, but I'd rather not be stuck in a loop for 2 hours checking for foo.txt.

Is there a way to have the script give up after, oh say 30 seconds?


sorry, this is a bash script. Should have mentioned.

November 23rd, 2009, 08:35 AM
deleted, didn't read the question right

November 23rd, 2009, 09:07 AM
It wouldn't be exact, but I suppose you could just check the starting time and do something like:

while ($starttime < ($starttime+30)) {
do stuff

November 23rd, 2009, 09:11 AM
i'm not sure if there is a built-in function that handles this

you can do a rough kind of time check through the `date +%s` command, where you the output to one variable in the beginning and keep it static (this is your reference point), then have another variable keep running `date +%s` until X number of seconds have passed (you reference with the first variable).

something like this:


while [ $temp != 0 ]; do
if [ $temp == 1 ]; then #ensures that this is only done on the first pass
starttime=$(date +%s) #start time of the loop
runningtime=$(date +%s) #this is your running time
if [ $runningtime == $[$starttime+30] ]; then #if running time = start time + 30 seconds
echo "30 seconds have elapsed in the while loop"

just adjust depending on how many seconds you want it to run

November 23rd, 2009, 10:13 AM
Eh, I don't know what I was thinking when I wrote that. I was pretty stressed and on my way to the bus. I guess that's why.

Anyway, what I meant was more like this:

while ($currenttime < $starttime+30) {
do stuff