[Solved] If doesn't evaluate the first test


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] If doesn't evaluate the first test
# 1  
Old 12-18-2012
[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

Code:
#!/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"


Last edited by jimm; 12-19-2012 at 09:43 AM.. Reason: solved
# 2  
Old 12-18-2012
You can replace:
Code:
#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:
Code:
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
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 12-18-2012
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.
# 4  
Old 12-18-2012
So, is the output your getting:
Code:
Yes Yes
done

# 5  
Old 12-18-2012
almost,
Code:
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"
# 6  
Old 12-18-2012
OK bash string comparisons are case sensitive so Yes and yes are different.

You can either put:

Code:
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")
# 7  
Old 12-19-2012
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 [ ].
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to test that a string doesn't contain specific characters?

Hi ! :) I made this : #!/bin/bash rsa_dir="/etc/openvpn/easy-rsa/" rsa_key_dir="/etc/openvpn/easy-rsa/keys/" ccd_dir="/etc/openvpn/ccd/" regex_special_char='' cd $rsa_dir while read -p "Please can you enter the vpn's username : " username ] || ] || ] || ] do echo "Your entry... (10 Replies)
Discussion started by: Arnaudh78
10 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Custom actions in Thunar doesn't display at all.

I have came across a few websites stating some custom actions for Thunar - crunchbang ubuntu I tried inputting the stated commands to Thunar, but it doesn't display at all in mine. I tried "gksu thunar %f" ( Opens current folder with root permissions.) , but when I right click in a... (0 Replies)
Discussion started by: Hijanoqu
0 Replies

3. Shell Programming and Scripting

[solved] awk: test assoc. array for content

Hi all, I am looking for a quick/short way in awk to check if an associative array has any content. I know I can split() it to an indexed array and check if the 1st element is set, or cycle through it with something like for( ele in arr ), but I want to avoid that, as I am looking for a shorter... (3 Replies)
Discussion started by: zaxxon
3 Replies

4. UNIX for Dummies Questions & Answers

[Solved]Can anyone tell me why -H flag with sudo doesn't switch to the target user's home directory?

I have checked the man page ,which says : The -H (HOME) option sets the HOME environment variable to the homedir of the target user (root by default) as specified in passwd(5). By default, sudo does not modify HOME But I have tried below command: #... (1 Reply)
Discussion started by: Michaelw321
1 Replies

5. Shell Programming and Scripting

[Solved] Script doesn't work..help?

hi, i am trying to run this script.the name of script is final.sh after i run it: #./final.sh & i grep the command # ps -a | grep bash and i see more then one processes runing 3!! Please use code tags how can i solve this problem? my target script must always run in... (8 Replies)
Discussion started by: zigizag
8 Replies

6. Shell Programming and Scripting

[Solved] BASH - chaining TEST and COMMAND with && and II

Can you explain what this line of script is doing. What I have understood is : -- variable C is the name of a software which is either not installed, so it must be installed or allready installed and then should be update if newer version found -- branch B="$B $C" is to install the software --... (4 Replies)
Discussion started by: jcdole
4 Replies

7. Shell Programming and Scripting

Value of variable is NULL, but test doesn't seem to recognize

Hello, Unix-forums! My problem: read -p "Enter any number, please" number sleep 1 echo $number | tr -d 0-9 test -z $number && echo "Thank you" || echo "This is not a number"Test always displays "This is not a number". It doesn't matter if I entered a or 1. But if I order echo... (2 Replies)
Discussion started by: intelinside
2 Replies

8. Shell Programming and Scripting

[Solved] Bash test 2 variables to see if ones greater by n

Experts, I have a bash shell script that generates 2 variables that have the current minute and a minute from a log file. Can someone please show me the best way to test if the minutes stray by 5. So basically if: This is ok: Last Fitting Min ============= 02 Current Minute =============... (2 Replies)
Discussion started by: jaysunn
2 Replies

9. Programming

Kernel module - How to test if file doesn't exist

Hey, I'm currently getting into some kernel module progamming. As a little exercise I want to read the headers out of an ELF file. My code is very simple, here is the important part: struct file *fp; /* ... */ fp = filp_open("some/file/on/my/pc", O_RDONLY, 0); if(fp == NULL) { ... (15 Replies)
Discussion started by: disaster
15 Replies

10. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies
Login or Register to Ask a Question