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