변규섭 변규섭 - 26 days ago 9
Bash Question

why do I get an error 'invalid arithmetic operator'?

I am trying to parse yaml to Json.
and I'd like to set the array that has string index.

when I v tried it I got an error

here is my source and error

keys=$(echo $ci_json | jq 'keys')
key_array=($keys)
ARR=()

for raw_key in ${key_array[@]}; do
if [ $raw_key = '[' -o $raw_key = ']' ]; then
continue;
else
key=$(echo $raw_key | sed -e 's/"//g' -e 's/,//g')
value=$(echo $ci_json | jq .$key)$'\t'
ARR[$key]=$value
fi
done


Error : localhost.localdomain: syntax error: invalid arithmetic operator (error token is ".localdomain")

This error appeared at "ARR[$key]=$value"

anybody helps me?

Answer
 Error : localhost.localdomain: syntax error: invalid arithmetic operator (error token is ".localdomain")

This happens on ARR[$key]=... when ARR is an indexed array (not associative) and the value of key is localhost.localdomain. Indexed arrays should use numeric subscripts, not arbitrary strings.

If you want to use string indexes with ARR, you must declare it as an associative array, which is supported as of Bash 4:

declare -A ARR