Waiting for wildcard filename to exists in while loop

Hi Experts,

We are developing a script which will wait for the trigger file(with datetime in the trigger file name).
But the problem is when I use 'while' loop to wait for the file, it waits for the filename with wilcard in it that is wait for 'Trigger*.done' file. :eek:
Below is the script

#!/bin/ksh

path="/home/user/"
triggerfile="Trigger*.done"
counter=0
max_counter=12
actualfile=${path}${triggerfile}

while [ ! -f ${actualfile} ]; do
let counter=counter+1
if [ $counter -ge $max_counter ]
then
 echo "email reached max counter"
else
 sleep 5
fi
done

This script works when I touch 'Trigger*.done' file in the source. Is there anyway to loop until the filename(with wildcard character) exists?:confused::confused:

Appreciate your help!!

Thanks

I am not sure if i understand you correctly: you want to wait for a file that has a (literal) wildcard character in its name instead of having that wildcard character expanded?

You can easily get this by enclosing the variable into double quotes:

# ls
filea fileb filec
# file="*"
# echo "$file"
*
# echo $file
filea fileb filec

It is the same in your loop:

while [ ! -f "${actualfile}" ]; do

but the problem already comes from a previous line, where you use your variable unprotected (unquoted):

actualfile=${path}${triggerfile}

You should generally protect your variables and only use them without quotes in the few cases where you need them to be interpreted:

actualfile="${path}${triggerfile}"

But there are other problems with your code you probably are not aware of. Here is your code after a bit of straightening (you should not name a variable "path", btw., it is too easy to be confused with the system variable PATH):

#!/bin/ksh

triggerpath="/home/user"
triggerfile="${triggerpath}/Trigger*.done"
counter=0
max_counter=12

while [ ! -f "$triggerfile" ] ; do
     (( counter += 1 ))
     if [ $counter -ge $max_counter ] ; then
          echo "email reached max counter"
     else
          sleep 5
     fi
done
  1. You have no "sleep" command when in the TRUE-branch of the "if". Instead of testing every 5 seconds as before it tests as often as it can once "$counter" reaches "$max_counter". You may want to move the "sleep"-command outside the if:
while [ ! -f "$triggerfile" ] ; do
     (( counter += 1 ))
     if [ $counter -ge $max_counter ] ; then
          echo "email reached max counter"
     fi
     sleep 5
done
  1. This is a remote chance, but you increment the counter continually. Since you have no "sleep" between tests once "$counter" reaches threshold you might eventually increment over the size of shell numerics, which are integers.
while [ ! -f "$triggerfile" ] ; do
     if [ $counter -eq $max_counter ] ; then
          echo "email reached max counter"
     else
          (( counter += 1 ))
     fi
     sleep 5
done

I hope this helps.

bakunin

It's always dangerous to want a wildcard character as part of a filename, but you can do this:-

until [ -f Trigger\*.done ]
do
 ......

You must make sure that you escape every usage of the file name correctly, and if you are setting it as a variable, you might need to double escape it, i.e.

file="Trigger\\\*.done"
until [ -f Trigger\*.done ]
do
 ......

... so you escape the escape character too so that doesn't get interpreted on setting the variable.

I still worry that you may make a mistake with serious consequences. You might be better using a file called All_Trigger.done instead for clarity and to minimise the risks, especially of any maintenance to your code later on.

Robin

Thanks all for the reply!!

Sorry for the confusion. Actually our file name will have datetime for e.g Trigger_09122014103030.done.

I am creating script which will wait until this file appears. Since this file is datetime appended, I can not give exact file in while loop(while running daily), that's reason the I am trying to check if the file name with Trigger*.done exists.

Command :-

while [ ! -f "${actualfile}" ]; do

When I add the 'Trigger_09122014103030.done' in the mentioned source location (when script is in while loop) it does not come out of the while loop
But when I add 'Trigger*.done' it comes out of the while loop.
I think 'while' command searches for the exact file name

Appreciate all your help!!

Thanks

The while doesn't - it couldn't care less. The test -f does - and has to. It needs exactly one single file, or it will issue an error msg. Don't use the double quotes as they prevent the shell from evaluating the wild card.