0

this script is working properly

stat1=$(systemctl list-units --type=service | grep gravity | grep gcr | awk {'print $1'} | xargs systemctl status | grep Active | awk {'print $2'})
echo "$stat1"
if [ $stat1 == 'active' ]
then
  echo " >> Gravity service is running ..."
else
  echo " >> Gravity service not running ..."
fi

this one isn't

stat2=$(gravity status | grep status | awk {'print $3'})
echo "$stat2"
if [ $stat2 == 'active' ]
then
  echo " >> Gravity service is running ..."
else
  echo " >> Gravity service not running ..."
fi

stat2 script

the only difference between them is the input for stat variable on this script is white, and the other one is green. i also tried using if [[ ]] , and changed '' to "" for the stat var , none of this worked Please help me understand how to make it work.

tried this :

enter image description here

the result remains the same.

Eyalu
  • 1

1 Answers1

6

I strongly suggest you rather use the is-active query from systemctl

systemctl is-active gravity

which returns the current service status, or:

sytemctl --quiet is-active gravity

which just exits with 0 if active and non-zero otherwise, which means you could use

if sytemctl --quiet is-active gravity; then
  echo " >> Gravity service is running ..."
else
  echo " >> Gravity service not running ..."
fi

From the manual:

is-active PATTERN…

Check whether any of the specified units are active (i.e. running). Returns an exit code 0 if at least one is active, or non-zero otherwise. Unless --quiet is specified, this will also print the current unit state to standard output.


Now for the issue itself:

Green color is passed as part of the string and thus the match fails. Compare it to this:

if [ "$(echo 1 | grep --color=yes 1)" = 1 ]
then echo equal
else echo different
fi
#different

if [ "$(echo 1 | grep --color=no 1)" = 1 ] then echo equal else echo different fi #equal

See how it is different in the first case and identical in the second one because [ stra == strb ] tests for exactly matching strings.

You could make a regex match to avoid this problem via

if [[ $(echo 1 | grep --color=yes 1) =~ 1 ]]
then echo equal
else echo different
fi
#equal

but be aware that the regex MIGHT match the color coding (not in the case of words like "active", though). Note how this needs the extended test [[ ]] from bash.

Some programs are smart enough to not forward the color to a pipe, e.g. grep, but one may force it:

echo 1 | grep 1 | cat -A
1$
echo 1 | grep 1 --color=yes | cat -A
^[[01;31m^[[K1^[[m^[[K$

So this is how the colorizing of 1 in bold red looks like. More on bash colors here.

ilkkachu
  • 138,973
FelixJN
  • 13,566
  • Hi, the systemctl output is actually working , the other one that colored green is not – Eyalu Jan 13 '22 at 21:17
  • Yes, just added some info on how colors are part of the string. – FelixJN Jan 13 '22 at 21:18
  • 5
    @Eyalu The color is in fact a set of invisible control characters that are part of the data in the string. This means that the string can't be compared without first removing the color (or better yet, without even adding the color in the first place). – Kusalananda Jan 13 '22 at 21:26
  • [[ $(...) == *active* ]] would also work (glob pattern instead of regex). – ilkkachu Jan 14 '22 at 14:39