[Solved] If doesn't evaluate the first test

Good afternoon,
I am tearing hair out over this. It should be so simple. I have an if statement to evaluate whether or not replication is working. I am testing variables from mysql to see if they are both "Yes". I have put some echo statements in to see how far the code proceeds. It always seems to skip the initial test (or evaluate it as false) and run to the end of the script. I could really use another pair of eyes on this.
Thanks
Jim

#!/bin/bash

# variables
host="ipaddr"
user="user"
pass="password"
var="Yes"

#Connect
out=$(mysql -h $host -u$user -p$pass -se "show slave status")
echo $out > /tmp/working.txt
awk '{print$11, $12, $16, $17, $19, $20}' /tmp/working.txt >> /tmp/variables
rm -f /tmp/working.txt
read log pos io sql error query < /tmp/variables

echo $io $sql

if [$io = "Yes" -a $sql = $var]; then
rm -f /tmp/variables
echo "made 1"
        else
mail -s "replication broke" email@gmail.com < /tmp/variables
echo "hit 2"
fi
rm -f /tmp/variables
echo "done"

You can replace:

#Connect
out=$(mysql -h $host -u$user -p$pass -se "show slave status")
echo $out > /tmp/working.txt
awk '{print$11, $12, $16, $17, $19, $20}' /tmp/working.txt >> /tmp/variables
rm -f /tmp/working.txt

with:

mysql -h $host -u$user -p$pass -se "show slave status" | awk '{print$11, $12, $16, $17, $19, $20}' > /tmp/variables

I think you are missing a space between [ and $io

1 Like

Thanks Chubler_XL that will save a few lines. I had it expanded like that so I could stick "echo" in as needed to see where It got on each try. I also just ran with a space after and before each [ ], no joy.

So, is the output your getting:

Yes Yes
done

almost,

yes yes
hit 2
done

---------- Post updated at 06:07 PM ---------- Previous update was at 06:05 PM ----------

I forgot to mention, last time I ran it with the spaces by the [ I got a bash error "command Yes not found"

OK bash string comparisons are case sensitive so Yes and yes are different.

You can either put:

shopt -s nocasematch

before the compare to ignore case and revert to case sensitive with shopt -u nocasematch

or compare with the correct case version (eg "yes" instead of "Yes")

Thanks, and yes I was careful about that and am only trying to do a comparison of Yes == Yes.

---------- Post updated 12-19-12 at 08:00 AM ---------- Previous update was 12-18-12 at 06:37 PM ----------

Just a bump here. I thought that this would be easy and some code wizard would just tell me "Oh, you forgot a comma here". Maybe I'm not as crazy as I thought.
Jim

---------- Post updated at 08:41 AM ---------- Previous update was at 08:00 AM ----------

I solved this one and am posting the solution for anyone else to benefit from. The problem is I don't yet know why it works
I changed the Yes == Yes test to a negative and added the space after and before the [ ].

if [ $io != $var -o $sql != $var ]; then
mail -s "replication broke" emailaddr@gmail.com < /tmp/variables
fi

The space I had tried previously with no good result so why changing to a negative made any difference I don't know.