Last active
January 15, 2016 02:26
-
-
Save stevekuznetsov/11ec05a03253f5069fab to your computer and use it in GitHub Desktop.
attempt to get associative array bugs to occur
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
| #!/bin/bash | |
| set -o errexit | |
| set -o nounset | |
| set -o pipefail | |
| source testlib.sh | |
| testadd 'testkey' 'testval' | |
| echo "${myarray['testkey']-}" |
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
| #!/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