# Simple bourne shell based key-value store if [ -z "${__kvs_sh_loaded}" ] then __kvs_sh_loaded=yes t="$(printf '\t')" [ "${kvs}" ] || { cat < "${kvs}" # Add or replace a value stored for a given key and id in the kvs kvs_set() { [ "${1}" -a "${2}" ] || return 255 local id="${1}" local var="${2}" local val="${3}" printf "%s\t%s\t%s\n" "${id}" "${var}" "${val}" >> "${kvs}" } # Fetch the most recent value stored for a given key and id from the kvs kvs_get() { [ "${1}" -a "${2}" ] || return 255 local id="${1}" local var="${2}" grep "^${id}${t}${var}${t}" "${kvs}" | tail -n 1 | cut -d"${t}" -f3- } # Does the kvs have any entries for a given ID? kvs_has_id() { [ "${1}" ] || return 255 local id="${1}" grep -q "^${id}${t}" "${kvs}" } # Does the kvs have a given key for a given ID? kvs_has_key() { [ "${1}" -a "${2}" ] || return 255 local id="${1}" local var="${2}" grep -q "^${id}${t}${var}${t}" "${kvs}" } # Remove a given key with a given ID from the kvs kvs_unset() { [ "${1}" -a "${2}" ] || return 255 local id="${1}" local var="${2}" grep -v "^${id}${t}${var}${t}" "${kvs}" > "${kvs}.tmp" && mv -f "${kvs}.tmp" "${kvs}" || rm -f "${kvs}.tmp" } # Remove all keys with a given ID from the kvs kvs_unset_all() { [ "${1}" ] || return 255 local id="${1}" grep -v "^${id}${t}" "${kvs}" > "${kvs}.tmp" && mv -f "${kvs}.tmp" "${kvs}" || rm -f "${kvs}.tmp" } fi