Skip to content

Instantly share code, notes, and snippets.

@esweeney-cg
Created October 10, 2022 19:52
Show Gist options
  • Save esweeney-cg/d2fa78fb0e692c84b407aaa739579568 to your computer and use it in GitHub Desktop.
Save esweeney-cg/d2fa78fb0e692c84b407aaa739579568 to your computer and use it in GitHub Desktop.
associative array via temp fs 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