if-statement troubles

I try to compare the day and month of someones birthday with the day and month of today, so my little bash script can send a mail to the person that has its birthday that day.

The first line of the file birthdays looks like this:
firstname,lastname,01/01/1990,....

The variable birthday's value is: 01/01
The variable today's value is: 06/08

Yet when i compare them in the if statement you see below, he still executes the commands in the if-block as if the two values were the same.

I tried to change my if statement in a million ways but my output always is "Happy birthday".

Can someone try to explain why this if statement doesnt work?

cat birthdays | while read line; do
birthday=`echo $line | sed 's/^.*,.*,\(..\/..\)\/.*$/\1/g'`
today=`date '+%d/%m'`
if [ $today=$birthday ]
then
echo "Happy birthday"
echo ""
fi

---------- Post updated at 01:36 PM ---------- Previous update was at 01:35 PM ----------

and of course my code ends with "done"

I don't know what shell you're using but in Bash that if statement needs to look like this:

if [ $today == $birthday ]

Easy enough, but it worked. My book said one = should be enough

You are right, doc.arne. One '=' is enough. In fact, some borne shells don't work with two.

But you need spaces around the equal sign in a comparison.

if [ $today = $birthday ]

Also, I don't think you want the g flag at the end of the sed expression, in case there happen to be two dates on the line. Although I'm not sure what would happen since you are only printing \1.

birthday=`echo $line | sed 's/^.*,.*,\(..\/..\)\/.*$/\1/g'`

Test (=[) is builtin command = not bracket. Usually two mistakes using test command:

  • variable is empty => no argument => error
  • no argument separator between arguments - it's commandline, not programlanguage testing with brackets.

Solution: make always string = string length 0 is something = good argument and remember argument delimeters. Ex. in this case test command need 4 arguments: value operator value ]

If you use test command using test (not [ ), then you don't need last argument ].
Yes little funny, but command is exactly the same.

Generic solution for shells (sh, bsh, ksh, bash, zsh, ...):

[  "$today"  =  "$birthday"  ] 
#or
test  "$today"  =  "$birthday" 

more testing

---------- Post updated at 07:27 PM ---------- Previous update was at 07:23 PM ----------

There are also some other test commands, [[ and ((. Syntax are little different as [ / test.