error in if-else condition

Hi..

Im using the following script to find whether the present day is monday or not. If it is monday it has to do a specific set of things.

#!/bin/sh
Present_Date=`date`
LOAD_DAY=`date -d "$Present_Date" | cut -d " " -f 1`
echo $LOAD_DAY
if [ $LOAD_DAY = "Mon" ];then
echo "Monday"
#statements;
else
echo "Not Monday"
#statements;
fi

I have saved this script as 'test'. When i run this script using the command 'sh.test' i always get the following o/p with an error.

$ sh test
Mon
test: line 9: syntax error near unexpected token `fi'
test: line 9: `fi'

Im running this script in Linux machine. Can anyone please advice why this error occurs?

I copy/pasted your script, and don't get the same result. Perhaps you have an incorrect quote or space somewhere which got lost in translation when you pasted the script here? (Running Ubuntu 8.04 / bash.)

For what it's worth, the duplicated date commands are redundant; you can simply omit the calculation of Present_Date and even the cut with date +%a (or if you actually want the full weekday name, date +%A)

Im still getting the error. :frowning:

Following is the machine details where i run the script.

$ uname -a
Linux inafplxquas11 2.6.9-55.ELsmp #1 SMP Fri Apr 20 17:03:35 EDT 2007 i686 athlon i386 GNU/Linux

Is this error anything to do with the version of linux i am using?

No, it's not related to the kernel. It could have to do with the version of your shell, but more likely it's something like an invisible or incorrect character in the input which you cannot easily copy/paste here.

If you copy+paste back what you posted above, do you still get the error?

If so, can you run it with sh -vx scriptname and post the result here?

Hi ,

I copy+pasted the code which i had posted earlier. It dint work.
I tried 'sh -vx' . I have pasted the result below.

$ sh -vx test
#!/bin/bash
LOAD_DAY=`date +%a`
date +%a
++ date +%a
+ LOAD_DAY=$'Mon\r'
echo $LOAD_DAY
+ echo $'Mon\r\r'
Mon
if [ `date +%a` = "Mon" ];then
echo "Monday"
#statements;
else
echo "Not Monday"
#statements;
fi
test: line 10: syntax error near unexpected token `fi'
test: line 10: `fi'

The problem here seems like the carriage return part as u r copy pasting the script.

Thanks
namish

Just a W.A.G.:

Don't name the script test.