Bash - Loop function with specific period of time

I have a working function that checks if a file exists in a specific directory then calls another function that moves some other files, if the file does not exist, the functions calls itself. I cant calls the move files function before check if the main file exist in the proper directory.
function WORK {
cd $directory
FILENAME='main_filename_$var.txt'
if [ -f $FILENAME ]; then
  move_files; #calls another function
  echo "Main File Processed"
  echo "Files Moved"
  else 
  echo "Main file wasnt processed yet..."
  echo "Waiting..." # should wait before continous
  WORK;
fi
}

Open in new window

But I would like to use a better function, I am trying to find something that would precisely wait for some period (e.g one minute) and then calls the function again.

I am afraid this loop might crash if it gets too long. Its working because its not planned to get too long, but would be better avoid this risk.

So, instead of calling the same funtion, I should call something else, Maybe calling a function TIME (just a draft),
function TIME {
minute=$(date +"%M")
one_more=`$minute + 1`
while [ `$(date +"%M")` =! `$one_more` ]
do
echo "Waiting..."
done
WORK;
}

Open in new window


Any suggestion are very welcome.

Thanks!
joaotellesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
sleep 60
0
joaotellesAuthor Commented:
sleep 60
Ok, I will try.

I will also test this,

for file in $directory
do
	if [ "${file}" == "main_filename.txt" ]
	then
		move_files;
  		echo "Main File Processed!"
  		echo "Files moved"
		break
	fi
done

Open in new window

0
skullnobrainsCommented:
you can also cron your script every minute and let it just die when it has nothing to do (when the master file is not there)

----

you can also use inotifywait to wait until the file is created
something like this might do

inotifywait -m -e moved_to $directory | fgrep main_filename.txt | while read line
do move_files
done

inotifywait will wait for files to be moved to $directory and print a line for each moved file
grep makes sure a line is output if and only if the filename matches (with a lazy ereg)
0
joaotellesAuthor Commented:
for file in $directory
do
	if [ "${file}" == "main_filename.txt" ]
	then
		move_files;
  		echo "Main File Processed!"
  		echo "Files moved"
		break
	fi
done

Open in new window


It does not work, it looks for every file in the directory and if the file isnt found it ends whe loop.
0
skullnobrainsCommented:
while :
do
  test -e $directory/main_filename.txt && move_files && echo files moved
  echo waiting for 60 seconds
  sleep 60
done
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.