Skip to content

Instantly share code, notes, and snippets.

@jeffreyiacono
Created October 23, 2012 06:51
Show Gist options
  • Select an option

  • Save jeffreyiacono/3937301 to your computer and use it in GitHub Desktop.

Select an option

Save jeffreyiacono/3937301 to your computer and use it in GitHub Desktop.
db.sh
#!/bin/sh
# via: http://capotej.com/blog/2012/10/07/an-embedded-key-value-store-for-shell-scripts/
DBFILE=example.db
put(){
echo "export kv_$1=$2" >> $DBFILE
}
del(){
echo "unset kv_$1" >> $DBFILE
}
get(){
source $DBFILE
eval r=\$$(echo "kv_$1")
echo $r
}
list(){
source $DBFILE
for i in $(env | grep "kv_" | cut -d= -f1 ); do
eval r=\$$i; echo $(echo $i | sed -e 's/kv_//') $r;
done
}
## cmd dispatch
if [ ${1:-0} == "set" ]; then
put $2 $3
elif [ ${1:-0} == "get" ] ; then
get $2
elif [ ${1:-0} == "list" ] ; then
list
elif [ ${1:-0} == "del" ] ; then
del $2
else
echo "unknown cmd"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment