Getting a strange error here. I have some json structured like this:
BACKUP_REPOS='''
[
{"name":"my.server1", "hash":"server1-hash"},
{"name":"my.server2", "hash":"server2-hash"},
{"name":"my.server3", "hash":"server3-hash"}
]
'''
What I am attempting to do is generate a selectable menu based on that using jq
with the following function:
# generate the server menu
server_menu( ) {
# hold some arrays
declare -a _name _hash;
while read -r _server _pw; do
# hold the selected item
_name[$_server]=$_server;
_hash[$_server]=$_pw;
# end the loop generation
done < <( jq -rc '.[] | "\(.name) \(.hash)"' <<< "$BACKUP_REPOS" )
# now loop the resultset
select _server in "${!_name[@]}"; do
[ -n "$_server" ] && break;
done;
# return the server and hash
SELECTED_SERVER=${_name[$_server]};
SELECTED_HASH=${_hash[$_server]};
}
Now, this works perfectly fine in Ubuntu 18+, it generates an error on my Mac BigSur in Terminal. The error is: syntax error: invalid arithmetic operator (error token is ".server1") on line 82
, which in my full code file is this line: _name[$_server]=$_server;
Where am I going wrong here? At this point, my only concern is getting this script to run on my Mac... I'm ok with keep multiple copies of it
TIA
-A
(associative) rather than-a
(indexed) as per jesse_b's answerwhile
loop:eval "$( jq -r '.[] | (.name|@sh) as $n | (.hash|@sh) as $h | "_name[\($n)]=\($n)", "_hash[\($n)]=\($h)"' <<<"$BACKUP_REPOS" )"
. In any case-c
is not needed withjq
there.