call more options on if

Hi all,
I'm trying to make a script that will help me a lof on some configuration,
what this should do is to check the files if they exist ,if not the script will send me a mail to notify me that the files does not exist,

but when i call the script to test it,like this

sh /test/test_script.sh  /test/test_

it says to many arguments ,
and it checks only for the first date format,and skips the other,
how can this be done to check al the date formats and to send me a single mail not 5 mail?
any suggestions are very welcome
thanks in advance.

#!/bin/sh
{
FILE=$1
TODAY1=`date +%Y-%m-%d`
TODAY2=`date +%Y_%m_%d`
TODAY3=`date +%Y%m%d`
TODAY4=`date +%y-%m-%d-%H-%M`
TODAY5=`date +%y%m%d%H%M`
if [ -e $FILE$TODAY1* ] or [ -e $FILE$TODAY2* ] or [ -e $FILE$TODAY3* ] or [ -e $FILE$TODAY4* ] or [ -e $FILE$TODAY5* ] ;
 then
  echo $FILE  File Exists
else
  echo "$FILE File not Found" | mail -s 'server-test' my.account@my.domain.com
 fi
}

Neither * nor -e nor or work that way. You can't cram more than one file into -e, it won't work. By 'or' you probably meant '||'.

I'd try a loop instead of a huge or-statement, and put the files found into the $1 $2 ... list, then check if the first exists.

As a useful side-effect, the files being looked for will end up individually in $1, $2, ...

PREFIX="$1"
for D in "%Y-%m-%d" "%Y_%m_%d" "%Y%m%d" "%y-%m-%d-%H-%" "%y%m%d%H%M"
do
        TODAY="`date +$D`"
        set -- ${PREFIX}${TODAY}*
        # && is a short-form if/then.  If "$1" exists, then break out of the loop.
        [ -e "$1" ] && break
done

if [ -e "$1" ]
then
        echo "Found $# files for today:  $*"
else
        echo "No files found for today"
fi
1 Like

Thanks you very much,
very useful this method and super fast,
i was calling my first script with the date pattern at the and of the file like this

sh /test/test_script.sh  /test/test_`date +%Y_%m_%d`

but will definitively use your suggestion,
thanks again.

Regards

Hi Corona688,
i tried to make some test with your suggestion,and it works when the file exists,
but the problem is when it doesn't exist,
because if the file doesn't exist the script is sending the mail to me with the latest file date format available, it is not sending the correct file date of the file,any ideas on how can i come around this?

Thanks in advance
.