1

I want to create a toggle key for a Gnome extension. An extension keeps up with its state in a property. I chose this extension for example purpose:

gnome-extensions show [email protected] | grep State

I was expecting to use the turned value and do a string compare, but I am having problems. The returned value does not seem to be a string.

#!/bin/bash

STATE="$(gnome-extensions show [email protected] | grep State)"
echo $STATE     # output: State: ENABLED

COMPARE="State: ENABLED"

# does not work
if [ "$STATE" = "$COMPARE" ]; then
    echo "the state is enabled"
fi

My guess is that the returned result is not a string and that the colon has an obvious meaning in this, but I can't figure out how to go about this.

7
  • maybe it's using a tab. Notice that echo $STATE (without quotes) will turn any run of blank characters into a single space. Example: foo=$'a:\tb'; echo $foo; [ "$foo" != "a: b" ] && echo no match
    – user313992
    Mar 25, 2020 at 18:46
  • FWIW, you don't need the $(...) and the STATE var at all. if gnome-extensions show [email protected] | grep -q '^State:[[:blank:]]*ENABLED$'; then ...; fi.
    – user313992
    Mar 25, 2020 at 18:53
  • @mosvy - Pardon my level, but I am not quite understanding it. Could you elaborate more to a beginner level? I am using the variables to do trial and error stuff.
    – Daniel
    Mar 25, 2020 at 18:58
  • 1
    echo $STATE will NOT tell you if STATE is State: ENABLED or State:<10 Spaces Here>ENABLED or State:<TabCharacter>ENABLED of which only the first will match.
    – user313992
    Mar 25, 2020 at 19:04
  • tl;dr The colon has nothing to do with it. Add the output of gnome-extensions show [email protected] | grep State | hexdump -C to your question, maybe someone else will explain it to you better ;-)
    – user313992
    Mar 25, 2020 at 19:12

1 Answer 1

0

With some hints I got from @mosvy I was able to figure it out. Just posting it to close the question with an answer.

Just had to look for the substring 'ENABLED'. More simple than I expected.

#!/bin/bash

STATE="$(gnome-extensions show [email protected] | grep State)"

# example output for STATE: 'State: ENABLED'

if [[ $STATE == *"ENABLED"* ]]; then
  echo "It's there!"
  gnome-extensions disable [email protected]
else
  echo "Not found!"
  gnome-extensions enable [email protected]
fi

Closing question....

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .