Comparison of two variables with IF loop

Hello,
My script is like :

#!/bin/sh -x
TODAY=$(echo `date +"%b%d"`)
FILE_DATE=$(ls -l test.sh | awk '{print $6$7}')
echo "file date=$FILE_DATE"
echo "date=$TODAY"
if ["$TODAY" == "$FILE_DATE"]];then
echo "file is correct"
else
echo "file is incorrect"
fi

and The Output is :

$ ./test.sh
+ + date +%b%d
+ echo Jan21
TODAY=Jan21
+ + ls -l test.sh
+ awk {print $6$7}
FILE_DATE=Jan21
+ echo file date=Jan21
file date=Jan21
+ echo date=Jan21
date=Jan21
+ [Jan21 == Jan21]]
./test.sh[6]: [Jan21:  not found
+ echo file is incorrect
file is incorrect

Please let me know where i am doing wrong.

Regards,
saurau

Your if statement has one opening [ and two closing ]]. You also need a space after [ and before ].

There's also an unnecessary use of echo and backticks:

TODAY=$(echo `date +"%b%d"`)

Could be:

TODAY=$(date +"%b%d")
1 Like

And a single = instead of ==

1 Like

Thanks, It worked :slight_smile: