All 1 entries tagged List

View all 5 entries tagged List on Warwick Blogs | View entries tagged List at Technorati | There are no images tagged List on this blog

July 19, 2010

adding and removing items from gconf lists

So the other day I found myself wanting to add and remove items from a gconf key of type list. Reading and writing the whole list is easy, but I couldn't find a method for adding or removing an item from the list, nor a helpful blog or forum post describing someone else's method. So I wrote a script. It could probably be more efficient and there's at least one circumstance under which it won't work that I've already thought of and subsequently forgotten. But here it is anyway.

#!/bin/bash

# adds or removes an item from a gconf list

# N.B if you specify add, then the item will be added regardless of whether it
# is already in the list. if you only want to add the item but only if it's not
# already in the list, then call the script once with remove and then again with add

name=$(basename $0)

if [ $# -lt 3 ];then
echo "Usage: ${name} gconf_key item add|remove";
echo "E.g.:"
echo "${name} /apps/cheese/selected_effects vertical-flip add";
exit 1;
fi

if [ ! "$3" = "add" -a ! "$3" = "remove" ];then
echo "${name}: third argument must be either 'add' or 'remove'.";
exit 1;
fi

key=$1

item=$2

current_value=$(gconftool-2 --get ${key});

current_value=${current_value/[/}
current_value=${current_value/]/}

if [ "$3" = "remove" ];then

new_value=$(echo $current_value | tr ',' '\n' | grep -v ^${item}$ |tr '\n' ',' | sed 's/,$//');
# note removal of trailing , if present
elif [ "$3" = "add" ];then

# only want a , in front of ${item} if there's something in the list already
if [ -n "${current_value}" ];then
new_value="${current_value},";
fi
new_value="${new_value}${item}";
fi

new_value="[${new_value}]";
#echo $new_value

gconftool-2 --set $key --type list --list-type=string "${new_value}";




Search this blog

Tags

RSS2.0 Atom
Not signed in
Sign in

Powered by BlogBuilder
© MMXXI