Last active
September 7, 2016 15:23
-
-
Save iolloyd/7ba974316ade37768ceebedf15eec72c to your computer and use it in GitHub Desktop.
Bash / zsh key value map
This file contains hidden or 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 | |
pre=$(basename $0) | |
# the -d flag makes a directory in place of a file | |
# the -t flag uses the pre variable as a template | |
dir=$(mktemp -dt ${pre}) | |
getmap() { | |
# Make sure we have both a map name and key | |
[ "$#" != 2 ] && exit 1 | |
cat "${dir}/$1/$2" | |
} | |
addmap() { | |
# Make sure we have a map name, a key and a value | |
[ "$#" != 3 ] && exit 1 | |
[ -d "${dir}/$1" ] || mkdir "${dir}/$1" | |
echo $3 >"${dir}/$1/$2" | |
} | |
###### | |
# Test | |
###### | |
# addmap "testmap" "foo" "bar" | |
# addmap "testmap" "woo" "waah" | |
# echo $(getmap "testmap" "foo") | |
# echo $(getmap "testmap" "woo") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment