0

I am trying to compare output of a command with a string.

# echo "$status"
Stateretired
# if [ "$status" == "Stateretired" ] ; then echo "Instance down"; else echo "Well nah"; fi
Well nah
#

Update:

Tried with single = but same results.

# echo "$status"
Stateretired

# if [ $status = "Stateretired" ] ; then  echo "Instance retired"; else  echo "Well nah"; fi
Well nah

Debug trace

With debug I saw a strange thing with comparison '[' Stateretired == 'Stateretired' ']'. Is it normal ? Hope its not.

++ grep State
++ sed -E 's/ +//g ; s/\|//g'
+ status='Stateretired'
+ echo 'Stateretired'
Stateretired
+ mystatus=Stateretired
+ '[' Stateretired == 'Stateretired' ']'
+ echo 'Instance not down'
Instance not down

color coding

In keep on debugging, I came to know that the color coding is causing this problem.

echo "$status" | tr -d '[:cntrl:]'
State[31mretired[0m

I am trying to find a way to remove them.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Raja G
  • 5,973
  • 14
  • 49
  • 82
  • 1
    This question already has an answer here: [How to compare strings in bash](https://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash) – H. Girit Dec 04 '18 at 06:59
  • I'm betting that your variable contains a whitespace or control character, such as a carriage return. What does `echo "[$status]"` print? – ruakh Dec 04 '18 at 07:04
  • @ruakh, `echo "$status" Stateretired` and `echo "[$status]" [Stateretired]` – Raja G Dec 04 '18 at 07:17
  • @ruakh: I assumed you retracted the close vote because OP's variable could have stray characters. – Inian Dec 04 '18 at 07:23
  • $status should be in double quotes "$status" – joesan Dec 04 '18 at 07:45
  • @Inian: I'm not quite sure how to interpret your comment; are you trying to ask why I reopened the question? If so, then: this OP was already correctly testing the strings for equality, so it didn't make sense to close this question as a dupe of one that gives only what (s)he already knows. – ruakh Dec 04 '18 at 16:35

5 Answers5

1

As you found yourself, the control characters are making the strings different.
You can try to remove the control characters. Another approach is skipping them:

status="State[control characters]retired[more control characters]"
if [[ "${status}" =~ State.*retired ]]; then
   echo "Instance down"
else
   echo "Well nah"
fi

This solution will fail when the status can contain values like State is not retired or Previous State was retired, now back to work.

Walter A
  • 19,067
  • 2
  • 23
  • 43
0

You should use = instead of ==

This should work!

# echo "$status"
Stateretired
# if [ "$status" = "Stateretired" ] ; then echo "Instance down"; else echo "Well nah"; fi
#
joesan
  • 13,963
  • 27
  • 95
  • 232
  • 1
    Why? `man bash` lists both `string1 == string2` and `string1 = string2` as equal. Since his question is tagged as [tag:BASH], compatibility with other shells cannot be a valid reason unless stated explicitly. – Micha Wiedenmann Dec 04 '18 at 08:39
0

As my output is in color, I tried with tr to confirm that.

echo "$status" | tr -d '[:cntrl:]'
State[31mretired[0m

And with search I came across a sed regex which will delete them sed -r 's/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g'

Below is my debug trace

++ grep State
++ sed -E 's/ +//g ; s/\|//g'
+ status='Stateretired'
++ echo 'Stateretired'
++ sed -r 's/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g'
+ status=Stateretired
+ echo Stateretired
Stateretired
+ mystatus=Stateretired
+ '[' Stateretired == Stateretired ']'
+ echo 'Instance Down'
Instance Down

Hope it helps.

Raja G
  • 5,973
  • 14
  • 49
  • 82
0

If you know exactly what color sequence to expect, then one option is to simply include it in the string you're comparing to:

if [ "$status" == $'State\x1B[31mretired\x1B[0m' ] ; then

where $'...' is a special quoting notation that supports backslash-escapes like \n and \xHH and so on; see the Bash Reference Manual, §3.1.2.4 "ANSI-C Quoting" for more information.

That said, you might want to check the documentation of the command whose output you're capturing; it might support some options to make the output format more scripting-friendly.

ruakh
  • 175,680
  • 26
  • 273
  • 307
-1

It is simple via:

test "a" = "a" && { echo "OK" ; }  || { echo "NO" ; }
SkateScout
  • 815
  • 14
  • 24