How to search file for a date variable?

Hello,

I'm trying to write a ksh script which will allow me to to search for yesterday's date within a rtf file. I just need someway to know if the date is in the file, echo results to a text file, and then mail that out.

The format of the date is mm/dd/yyyy.

I had to make a variable for the date I'm searching for since "yesterday's date" can vary.

If this script is ran 5pm-midnight EST I need to look for the current date.

If ran after midnight but before 5pm, I will need to look for yesterday's date.

I have simplified the file names and paths for compliance reasons, but right now I have:

SOURCE_PATH="/path"
SOURCE_FILE="filename.rtf"
 
 
THE_TIME_HR=$(date "+%H")
 
#Use yesterday's date if ran after midnight EST, use current date if ran before midnight EST

 
if [ $THE_TIME_HR -ge 17 ]; then
 YEST_DATE=$(date "+%m/%d/%Y") #yesterday's date US
else
 YEST_DATE=$(TZ=EST+24 date "+%m/%d/%Y")
fi
 
#Look for min date for file validity, if nothing is returned then file should be correct
grep ${YEST_DATE} ${SOURCE_PATH}${SOURCE_FILE)

if [ $? -ne 0]; then

echo "Yesterday's date in file, please correct and reload" > $SOURCE_PATH/file.txt
 
else
 
echo "Yesterday's date not found in ClearTran file" > $SOURCE_PATH/file.txt
 
fi
 
#end of script

I'm getting the error "syntax error at line 25 : `>' unexpected" (the line I bolded) after the second if statement when I try to echo results to a text file.

Does anyone know what the issue is? Thanks in advance!

Also let me know if I need to clarify anything, I'm a new programmer.

Jay

Pls correct these errors and test again:

grep ${YEST_DATE} ${SOURCE_PATH}${SOURCE_FILE}
                                             ^--- } in lieu of )
if [ $? -ne 0 ]; then
             ^--  here be space(s) 
2 Likes

It worked!

Thanks joeyg :slight_smile: