Skip to content

Instantly share code, notes, and snippets.

@stevekuznetsov
Last active January 15, 2016 02:26
Show Gist options
  • Save stevekuznetsov/11ec05a03253f5069fab to your computer and use it in GitHub Desktop.
Save stevekuznetsov/11ec05a03253f5069fab to your computer and use it in GitHub Desktop.
attempt to get associative array bugs to occur
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
source testlib.sh
testadd 'testkey' 'testval'
echo "${myarray['testkey']-}"
#!/bin/bash
function testadd() {
local key=$1
shift
local vals=$@
for val in ${vals}; do
testappend "${key}" "${val}"
done
}
function testappend() {
local key=$1
local val=$2
if ! echo "${myarray[${key}]-}" | grep -q "${val}"; then
myarray["${key}"]="${myarray[${key}]-} ${val}"
fi
}
if [[ -z "${myarray+x}" ]]; then
declare -A myarray
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment