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.
"$(gsettings get org.gnome.system.proxy mode)"
to get the output ofgsettings get org.gnome.system.proxy mode
.'gsettings get org.gnome.system.proxy mode'
is just the string'gsettings get org.gnome.system.proxy mode'
. – muru Oct 18 '18 at 05:39sh -c 'case $(gsettings get org.gnome.system.proxy mode) in *none*) mode="$1";; *) mode="$2";; esac; gsettings set org.gnome.system.proxy mode "$mode"' _ manual none
– muru Oct 18 '18 at 06:30