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"
You either want to pass those array elements as arguments to find
:
TESTP=(-path './var/*')
find . "${TESTP[@]}"
Or build a shell command line and ask the shell to interpret it (and the quotes in it):
TESTP='-path "./var/*"'
eval "find . $TESTP"
Or you could use the split+glob operator (which you inappropriately used by leaving your variable unquoted in your question) on a scalar variable:
TESTP='-path,./var/*'
set -f # no glob
IFS=, # split on ,
find . $TESTP
In your,
TESTP=( "-path" "\"./var/*\"")
find ${TESTP[@]}
${TESTP[@]}
is first expanded to the array element (-path
and "./var/*"
), and then each is subject to the split+glob operator (as you forgot to quote it), so with the default value of $IFS
, they would not be split, but because the second one contains a wildcard it could be expanded to the files whose name end in "
in the "./var
directory.
Most likely there's no ".
directory, so both arguments are left asis and passed to find
. And that would be the same as
find -path '"./var/*"'
That is, looking for files below the "./var
directory that end in a "
character.
Best Answer
You need to double quote it (and you should double quote variables in most case):
But don't use echo to print variables content, using printf instead: