if [ -z "${__kvs_sh_loaded}" ] then __kvs_sh_loaded=yes __kvs_sh_VERSION="KVS v0.1" __kvs_sh_DESCRIPTION="Simple bourne shell based key-value store" __kvs_sh_COPYRIGHT=$( cat <<"END_OF_COPYRIGHT" Copyright (c) 2000-2012, CyberLeo All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of the source code must retain the above copyright notice, this list of conditions, and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILTY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. END_OF_COPYRIGHT ) 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