4

My intended goal is to create a script that when run, will check the state of a setting, then change it to the opposite state. The setting in question is regarding the manual or disabled state of proxy settings in Ubuntu's settings.

I can manually obtain the state of this setting with the command:

gsettings get org.gnome.system.proxy mode

This will return auto, manual, or none. I don't use the first state, so let's ignore that from here on. Now, once the state is returned, my goal with the script is to then run either one of the following commands:

gsettings set org.gnome.system.proxy mode 'manual'
gsettings set org.gnome.system.proxy mode 'none'

The command it runs will be the state not currently set. To that end, I have attempted the following in a script:

if [ 'gsettings get org.gnome.system.proxy mode' = 'none' ]
then
gsettings set org.gnome.system.proxy mode 'manual'
notify-send "Manual"
else
if [ 'gsettings get org.gnome.system.proxy mode' = 'manual' ]
then
gsettings set org.gnome.system.proxy mode 'none'
notify-send "Disabled"
fi
fi

This is saved in a file with chmod +x applied. When run from the terminal, nothing happens. I just get a new line to await a new command.

Where have I gone wrong?

In addition, is it possible to get this to work on a single line, like so:

sh -c "if [ $(gsettings get org.gnome.system.proxy mode) = \"'none'\" ];then gsettings set org.gnome.system.proxy mode 'manual' && notify-send \"Manual\";elif [ $(gsettings get org.gnome.system.proxy mode) = \"'manual'\" ];then gsettings set org.gnome.system.proxy mode 'none' && notify-send \"Disabled\";fi"

Again, same (lack of) results.

hiigaran
  • 6,843

2 Answers2

1

The if statement is appropriate tool in this case, however, consider as alternative case statement for single-run of the command in question and arguably cleaner syntax.

case "$(gsettings get org.gnome.system.proxy mode)" in

    "'none'") gsettings set org.gnome.system.proxy mode "'manual'" 
              notify-send "manual";;
    "'manual'") gsettings set org.gnome.system.proxy mode  "'none'"
                notify-send "Disabled" ;;

esac

Note that the gsettings get command in question returns single-quoted strings so you do need to check for that.

As for your specific code, the issue is that you're doing string comparison, but you really need command-substitution $()

if [ "$(gsettings get org.gnome.system.proxy mode)" = "'none'" ]
then
    gsettings set org.gnome.system.proxy mode "'manual'"
    notify-send "Manual"
elif [ "$(gsettings get org.gnome.system.proxy mode)" = "'manual'" ]
then
    gsettings set org.gnome.system.proxy mode "'none'"
    notify-send "Disabled"
fi

This is your code edited. Note that the syntax also was not correct, there's no else if but there's elif in shell syntax. Additionally, the two tests call same command twice. This is not efficient. Use single command substitution as shown in Gavin's answer

0

Your problem is that you cannot use a command in an if/then statement.

Here is the fixed code:

#!/bin/bash
output=$(gsettings get org.gnome.system.proxy mode)
if [ "$output" = 'none' ]
then
    gsettings set org.gnome.system.proxy mode 'manual'
    notify-send "Manual"
else
    if [ "$output" = 'manual' ]
    then
        gsettings set org.gnome.system.proxy mode 'none'
        notify-send "Disabled"
    fi
fi