GrayedFox
GrayedFox

Reputation: 2553

BASH Variable to string comparison always fails

I've searched, and searched, and searched... but I just can't figure out why on earth this simple BASH function is failing.

The code:

# Function to quickly disable or enable proxy server, system wide
    proxee() {
        MODE=$(gsettings get org.gnome.system.proxy mode)
        echo $MODE

        if [ "$MODE" = "manual" ]
        then
            gsettings set org.gnome.system.proxy mode 'none'
            echo "Proxy Disabled"

        elif [ "$MODE" = "none" ]
        then
            gsettings set org.gnome.system.proxy mode 'manual'
            echo "Proxy Enabled"
        else
           echo "FAIL"
        fi
    }

Every time I try to run it I get the following output:

'none'
FAIL

I essentially just want to compare the variable I have declared with a string literal.

I am pretty new to bash scripting and I've read over 15 different answers from Stack Overflow (this seems to be a common problem) - but I just can't figure it out!

Any help is much appreciated.

Upvotes: 2

Views: 392

Answers (2)

Drako
Drako

Reputation: 768

just change this line:

elif [ "$MODE" = "'none'" ]

string returned to mode is 'none' not none Enjoy

Upvotes: 1

bmk
bmk

Reputation: 14137

The command gsettings get org.gnome.system.proxy mode returns 'none' including the quote signs (').

Therefore you have to include them into the comparison:

...
elif [ "$MODE" = "'none'" ]
then
...

Upvotes: 7

Related Questions