Created
October 10, 2022 23:09
-
-
Save navicore/fcfaaa7788b39fc0c34735534ddc94a9 to your computer and use it in GitHub Desktop.
associative array via file system in bash
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
| # | |
| # 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