Change this line:
if [[ "$TEST" == "ON Master" ]];
To this:
if [ "$TEST" == "ON Master" ];
Details
The issue is the use of [[ .. ]]
. The output is showing you the difference. Your value that you're getting for $STATUS
is not simply "On Master". It most likely contains other characters, that are most likely not printable so are not being seen.
[[ .. ]]
++ echo On Master
+ TEST='On Master'
+ [[ On Master == \O\N\ \M\a\s\t\e\r ]]
[ .. ]
++ echo On Master
+ TEST='On Master'
+ '[' 'On Master' == 'ON Master' ']'
The use of double square brackets ([[ .. ]]
) is discussed here on the TLDP Advanced Bash Scripting pages.
echo $STATUS
This line seems a little suspicious to me as well. I'd protect the contents of $STATUS
by wrapping it in double quotes as well.
TEST=`echo "$STATUS"`
Also I'd drop the back ticks ( `...` ) and use the $( ... )
notation instead for executing this command. This change is just a best practice and isn't part of your issue though.
TEST=$(echo "$STATUS")
Control characters in output (^[[92mON^[[0m Master)
Given you're seeing these control characters in your output (^[[92m
& ^[[0m
) I'm suspicious that the grep
command is introducing these into your output in the pipe. It may be that grep
is aliases to always include the --color
switch, I'd temporarily try calling the executable directly, and by pass any aliases that may be there. Just change the grep
to this, /bin/grep
.
The presence of these is what we suspected and is why the text was wrapping when you echo
the variable $STATUS
. These characters are unprintable, and change the color of the terminal to highlight matches that grep
has found.
The presence of these also explains why the =~
operator didn't match too. You were trying to match 'On Master'
with '^[[92mON^[[0m Master'
.
Lastly the colored output with the control characters may be coming from another tool before the grep
. I would need to see the actual output from /root/setup_ha
to confirm this, but I would be suspicious of that tool as well in producing these characters in the pipe stream.
Stripping the control characters
I found this U&L Q&A titled: Program that passes STDIN to STDOUT with color codes stripped?.
Use either of these methods to get rid of the control characters.
Perl
$ cmd-with-colored-output | perl -pe 's/\e\[?.*?[\@-~]//g'
Sed
$ cmd-with-colored-output | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
Variable expansion (the standard term is parameter expansion, and it's also sometimes called variable substitution) basically means replacing the variable by its value. More precisely, it means replacing the $VARIABLE
construct (or ${VARIABLE}
or ${VARIABLE#TEXT}
or other constructs) by some other text which is built from the value of the variable. This other text is the expansion of the variable.
The expansion process goes as follows. (I only discuss the common case, some shell settings and extensions modify the behavior.)
- Take the value of the variable, which is a string. If the variable is not defined, use the empty string.
- If the construct includes a transformation, apply it. For example, if the construct is
${VARIABLE#TEXT}
, and the value of the variable begins with TEXT
, remove TEXT
from the beginning of the value.
- If the context calls for a single word (for example within double quotes, or in the right-hand side of an assignment, or inside a here document), stop here. Otherwise continue with the next steps.
- Split the value into separate words at each sequence of whitespace. (The variable
IFS
can be changed to split at characters other than whitespace.) The result is thus no longer a string, but a list of strings. This list can be empty if the value contained only whitespace.
- Treat each element of the list as a file name wildcard pattern, i.e. a glob. If the pattern matches some files, it is replaces by the list of matching file names, otherwise it is left alone.
For example, suppose that the variable foo
contains a* b* c*
and the current directory contains the files bar
, baz
and paz
. Then ${foo#??}
is expanded as follows:
- The value of the variable is the 8-character string
a* b* c*
.
#??
means strip off the first two characters, resulting in the 6-character string b* c*
(with an initial space).
- If the expansion is in a list context (i.e. not in double quotes or other similar context), continue.
- Split the string into whitespace-delimited words, resulting in a list of two-strings:
b*
and c*
.
- The string
b*
, interpreted as a pattern, matches two files: bar
and baz
. The string c*
matches no file so it is left alone. The result is a list of three strings: bar
, baz
, c*
.
For example echo ${foo#??}
prints bar baz c*
(the command echo
joins its arguments with a space in between).
For more details, see:
Best Answer
Field splitting occurs after expansion, and
IFS
whitespace is ignored at the beginning and end of the whole input (to avoid creating fields before the first "logical" field or after the last one). Thus by the time it gets to field splitting, your first example iswhich is parsed into fields
one
,two
, andand three
; likewise your second example iswhich is also parsed into fields
one
,two
, andand three
.