I'm using GNU-Bash in my MingW terminal (% with%). I need to create a data dictionary to check if a new key was found and associate this new key with an index. For this, I think the best solution would be an associative array.
However, I can not make this array work!
For tests, I'm assigning incremental values to the keys, one key per line. For example, I expect to get the following key / value pairs for this entry:
./array_assoc_platonico.sh << EOL
> bash
> scripting
> is
> tought
> as
> bash
> is
> pretty
> EOL
bash:1
scripting:2
is:3
tought:4
as:5
pretty:6
The print does not matter to me, but the contents of my array should look something like this.
My script so far:
#!/bin/bash
NEXT_IDX=1
while read line; do
if [ "x${chaves[$line]}" = "x" ]; then
# então chave nova
chaves[$line]=$NEXT_IDX
NEXT_IDX=$(( NEXT_IDX + 1 ))
echo "$line:${chaves[$line]}"
fi
done
However, my output is:
./array_assoc_falho.sh << EOL
> bash
> scripting
> is
> tought
> as
> bash
> is
> pretty
> EOL
bash:1
When I give bash --version: GNU bash, version 4.4.12(1)-release (i686-pc-msys)
at the end of the reading, I get the following:
declare -a chaves=([0]="1")
Where am I wrong to use the associative array in bash?