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.)STATUS='ON Master'the script prints "CLUSTER CRITICAL"); most likely your/root/setup_hareturns something that weird...if [[ "${STATUS}" == "ON Master" ]];| grep ">HA State" | awk '{print $3}' | cut -c 2-you meanawk '{if(/>HA State/) print $3;}'? (I don't really see what should thecutdo, but it definitely is possible withawk.