I am trying to write a bash script which run a command and compare the result with another string.
#!/bin/bash -x
STATUS=`/root/setup_ha show --password-file=/root/password | grep ">HA State" | awk '{print $3}' | cut -c 2-`
TEST=`echo $STATUS`
if [[ "$TEST" == "ON Master" ]];
then echo CLUSTER CRITICAL
else
echo CLUSTER OK MASTER
fi
As the Original string is on two lines, I echo it in a new variable TEST. The New variable have the output of the command on one line.
Here is the Bash debug output :
++ /root/setup_ha show --password-file=/root/password
++ grep '>HA State'
++ awk '{print $3}'
++ cut -c 2-
+ STATUS='ON
Master'
++ echo 'ON' Master
+ TEST='ON Master'
+ [[ ON Master == \O\N\ \M\a\s\t\e\r ]]
+ echo CLUSTER OK MASTER
CLUSTER OK MASTER
I also tried the following test :
if [[ "$TEST" =~ "ON Master" ]]
The thing is bash is not able to compare the strings it is always false.
Any idea ?
EDIT :
Here is the output with the first answer :
+ STATUS='ON
Master'
++ echo 'ON' Master
+ TEST='ON Master'
+ '[' 'ON Master' == 'ON Master' ']'
+ echo CLUSTER OK MASTER
CLUSTER OK MASTER
Still not working ON seems weird on line 3, plus in my bash it takes green color !
if [ "$TEST" = "ON Master" ]; then ...
instead? (What version of Bash? I can't reproduce your problem on Bash 3.2.48 or 4.1.5.) – 200_success Oct 17 '13 at 10:25STATUS='ON Master'
the script prints "CLUSTER CRITICAL"); most likely your/root/setup_ha
returns something that weird... – umläute Oct 17 '13 at 10:25if [[ "${STATUS}" == "ON Master" ]];
– Rahul Patil Oct 17 '13 at 11:13| grep ">HA State" | awk '{print $3}' | cut -c 2-
you meanawk '{if(/>HA State/) print $3;}'
? (I don't really see what should thecut
do, but it definitely is possible withawk
. – peterph Oct 17 '13 at 13:21=
, not==
. – l0b0 Oct 21 '13 at 13:17=~
is for regular expressions, and works exactly like=
when used with a quoted regular expression. You'd have to use=~ ON\ Master
. – l0b0 Oct 21 '13 at 13:18