Skip to content

Instantly share code, notes, and snippets.

@navicore
Created October 10, 2022 23:09
Show Gist options
  • Save navicore/fcfaaa7788b39fc0c34735534ddc94a9 to your computer and use it in GitHub Desktop.
Save navicore/fcfaaa7788b39fc0c34735534ddc94a9 to your computer and use it in GitHub Desktop.
associative array via file system in bash
#
# BEGIN all purpose associative array via temp fs
#
prefix=$(basename -- "$0")
mapdir=$(mktemp -dt ${prefix})
trap 'rm -r ${mapdir}' EXIT
put() {
[ "$#" != 3 ] && exit 1
mapname=$1; key=$2; value=$3
[ -d "${mapdir}/${mapname}" ] || mkdir "${mapdir}/${mapname}"
echo $value >"${mapdir}/${mapname}/${key}"
}
get() {
[ "$#" != 2 ] && exit 1
mapname=$1; key=$2
cat "${mapdir}/${mapname}/${key}"
}
#
# END all purpose associative array via temp fs
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment