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.
The
if
statement is appropriate tool in this case, however, consider as alternativecase
statement for single-run of the command in question and arguably cleaner syntax.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
$()
This is your code edited. Note that the syntax also was not correct, there's no
else if
but there'selif
in shell syntax. Additionally, the two tests call same command twice. This is not efficient. Use single command substitution as shown in Gavin's answerYour problem is that you cannot use a command in an if/then statement.
Here is the fixed code: