To find files by matching a pattern in file name

Hi all,

I have to check whether certain files exist using a if statement. I have to check this by matching a pattern of filename: e.g.

if [[ ${SOURCE_DIR}/test.dat* ]]

This statement should be "true" if any files like test.dat11, test.dat22 etc are present in the source dir. However, this statement is checking only for "test.dat"

Can anybody tell me as to how to accomplish this?

Thanks in advance.

Try this:

file_exists()
{
  while : ; do
    [ -f "$1" ] && return 0
    shift       || return 1
  done
}

if file_exists test.dat* ; then
  ...
fi

If you do not need to test for files specifically you can do this too:

exists(){ [ -e "$1" ]; }
if exists test.dat* ; then
  ...
fi

In a test metacaracters like a wildcard will not work.
You can write it like this for example:

ls -1 test.dat* > /dev/null 2>&1
if [ $? -eq 0 ]; then
   echo ok
else
   echo error
fi