Created
February 22, 2017 01:45
-
-
Save zhouchangxun/3a5b36d88f31e32e0eff7c6983727b9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Notice special chars inside key like / will replace with _ eg. abc/def => abc_def | |
SED=/bin/sed | |
DB=kv.db | |
ensure_db_exist(){ | |
if [ $1 ];then | |
DB=$1 | |
fi | |
if [ ! -f $DB ];then | |
# "generating db file..." | |
touch $DB | |
fi | |
} | |
# $1 : key $2: db file name | |
kv_get(){ | |
ensure_db_exist $2 | |
key=`echo $1 | sed 's/\//_/g'` | |
# "debug: internal key: $key" | |
$SED -n "s/^${key}=\(.*\)/\1/p" $DB | |
} | |
# $1:key $2:value $3:db file | |
kv_set(){ | |
ensure_db_exist $3 | |
key=`echo $1 | sed 's/\//_/g'` | |
ori_value=`kv_get $key` | |
if [ $ori_value ] | |
then | |
# "debug: replace the old value $ori_value to $2" | |
$SED -i "s/^${key}=.*$/${key}=${2}/" $DB | |
else | |
# "debug add a new record" | |
echo "$key=$2" >> $DB | |
fi | |
} | |
# delete | |
# $1 : key | |
kv_del(){ | |
ensure_db_exist $2 | |
key=`echo $1 | sed 's/\//_/g'` | |
$SED -i "/^${key}=/d" $DB | |
} | |
kv_test(){ | |
echo "kv_set name xun ./test.db" | |
kv_set name xun ./test.db | |
echo "kv_get name ./test.db" | |
kv_get name ./test.db | |
echo "kv_del name ./test.db" | |
kv_del name ./test.db | |
echo "rm test.db -rf" | |
rm test.db -rf | |
} | |
# kv_test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment