1 # Simple bourne shell based key-value store
3 if [ -z "${__kvs_sh_loaded}" ]
12 Copyright - http://wiki.cyberleo.net/wiki/CyberLeo/COPYRIGHT?version=4
14 Set the following variables before sourcing this library:
16 kvs - Path and filename of the kvdb database file
22 [ -s "${kvs}" ] || echo "${t}${t}# KVSv0 id-key-value database" > "${kvs}"
24 # Add or replace a value stored for a given key and id in the kvs
26 [ "${1}" -a "${2}" ] || return 255
30 printf "%s\t%s\t%s\n" "${id}" "${var}" "${val}" >> "${kvs}"
33 # Fetch the most recent value stored for a given key and id from the kvs
35 [ "${1}" -a "${2}" ] || return 255
38 grep "^${id}${t}${var}${t}" "${kvs}" | tail -n 1 | cut -d"${t}" -f3-
41 # Does the kvs have any entries for a given ID?
43 [ "${1}" ] || return 255
45 grep -q "^${id}${t}" "${kvs}"
48 # Does the kvs have a given key for a given ID?
50 [ "${1}" -a "${2}" ] || return 255
53 grep -q "^${id}${t}${var}${t}" "${kvs}"
56 # Remove a given key with a given ID from the kvs
58 [ "${1}" -a "${2}" ] || return 255
61 grep -v "^${id}${t}${var}${t}" "${kvs}" > "${kvs}.tmp" && mv -f "${kvs}.tmp" "${kvs}" || rm -f "${kvs}.tmp"
64 # Remove all keys with a given ID from the kvs
66 [ "${1}" ] || return 255
68 grep -v "^${id}${t}" "${kvs}" > "${kvs}.tmp" && mv -f "${kvs}.tmp" "${kvs}" || rm -f "${kvs}.tmp"