Notes

Append to a dconf list and avoiding duplicates with existing entries

Edit on GitHub

Bash Scripting
2 minutes

I need to do this for a script i’m writing to add custom keyboard shortcuts. I get the existing list with

1gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings
['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/']

and now i need to add new entries to it and avoid duplicates with any previous custom$ shortcuts.

1gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "[<altered_list>]"

Adding values to an existing array

1arr=( "${arr[@]}" "new_element1" "new_element2" "..." "new_elementN")
2#Or
3arr+=( "new_element1" "new_element2" "..." "new_elementN" )

using command substituion

1CUSTOM_SHORTCUTS_LIST=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
2# ['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/']
3echo -e "\nCUSTOM_SHORTCUTS_LIST: \n${CUSTOM_SHORTCUTS_LIST}"
4
5NEW_SHORTCUTS_LIST="${CUSTOM_SHORTCUTS_LIST%]*}, 'blah']" # substituion
6
7echo -e "\nNEW_SHORTCUTS_LIST: \n${NEW_SHORTCUTS_LIST}\n"
8# ['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/', 'blah']

Here are some way people have done this when creating Gnome Terminal themes

 1# One Dark
 2# https://github.com/denysdovhan/one-gnome-terminal/blob/master/one-dark.sh
 3dlist_append() {
 4    local key="$1"; shift
 5    local val="$1"; shift
 6
 7    local entries="$(
 8        {
 9            "$DCONF" read "$key" | tr -d '[]' | tr , "\n" | fgrep -v "$val"
10            echo "'$val'"
11        } | head -c-1 | tr "\n" ,
12    )"
13
14    "$DCONF" write "$key" "[$entries]"
15}
1# Nord
2# https://github.com/arcticicestudio/nord-gnome-terminal/blob/develop/src/nord.sh
3append_profile_uuid_to_list() {
4  local uuid list
5  uuid="$1"
6  list=$(gsettings get "$GSETTINGS_PROFILELIST_PATH" list)
7  gsettings set "$GSETTINGS_PROFILELIST_PATH" list "${list%]*}, '$uuid']"
8}

Related