Skip to main content
added 84 characters in body
Source Link
Kusalananda
  • 356.3k
  • 42
  • 737
  • 1.1k
menu_select_server () { local server_json=file.json # JSON containing server config local name hash local hashes=() set -- # Read names into list of positional parameters # Read hashes into "hashes" array while IFS=$'\t' read -r name hash; do set -- "$@" "$name" hashes+=( "$hash" ) done < <( jq -r '.[] | [ .name, .hash ] | @tsv' "$server_json" )  # or: # jq -r 'map([ .name, .hash ] | @tsv)[]' "$server_json"  # Select wanted server local PS3='Select server: ' select SELECTED_SERVER; do [[ -n $SELECTED_SERVER ]] && break done # Get corresponding hash SELECTED_HASH=${hashes[REPLY-1]} } 
menu_select_server () { local server_json=file.json # JSON containing server config local name hash local hashes=() set -- # Read names into list of positional parameters # Read hashes into "hashes" array while IFS=$'\t' read -r name hash; do set -- "$@" "$name" hashes+=( "$hash" ) done < <( jq -r '.[] | [ .name, .hash ] | @tsv' "$server_json" ) # Select wanted server local PS3='Select server: ' select SELECTED_SERVER; do [[ -n $SELECTED_SERVER ]] && break done # Get corresponding hash SELECTED_HASH=${hashes[REPLY-1]} } 
menu_select_server () { local server_json=file.json # JSON containing server config local name hash local hashes=() set -- # Read names into list of positional parameters # Read hashes into "hashes" array while IFS=$'\t' read -r name hash; do set -- "$@" "$name" hashes+=( "$hash" ) done < <( jq -r '.[] | [ .name, .hash ] | @tsv' "$server_json" )  # or: # jq -r 'map([ .name, .hash ] | @tsv)[]' "$server_json"  # Select wanted server local PS3='Select server: ' select SELECTED_SERVER; do [[ -n $SELECTED_SERVER ]] && break done # Get corresponding hash SELECTED_HASH=${hashes[REPLY-1]} } 
added 113 characters in body
Source Link
Kusalananda
  • 356.3k
  • 42
  • 737
  • 1.1k

As jesse_b pointed out, the issue is that your array is not an associative array. The bash on macOS doesn't know how to deal with associative arrays, so consider rewriting it in zsh or use ordinary arrays. Alternatively, install an updated bash shell using e.g. Homebrew and correct the declare -a to declare -A.

As jesse_b pointed out, the issue is that your array is not an associative array. The bash on macOS doesn't know how to deal with associative arrays, so consider rewriting it in zsh or use ordinary arrays.

As jesse_b pointed out, the issue is that your array is not an associative array. The bash on macOS doesn't know how to deal with associative arrays, so consider rewriting it in zsh or use ordinary arrays. Alternatively, install an updated bash shell using e.g. Homebrew and correct the declare -a to declare -A.

added 5 characters in body
Source Link
Kusalananda
  • 356.3k
  • 42
  • 737
  • 1.1k
menu_select_server () { local server_json=file.json # JSON containing server config local name hash local hashes=() set -- # Read names into list of positional parameters # Read hashes into "hashes" array while IFS=$'\t' read -r name hash; do set -- "$@" "$name" hashes+=( "$hash" ) done < <( jq -r '.[] | [ .name, .hash ] | @tsv' "$server_json" ) # Select wanted server local PS3='Select server: ' select SELECTED_SERVER; do [[ -n $SELECTED_SERVER ]] && break done # Get corresponding hash SELECTED_HASH=${hashes[REPLY-1]} } 
menu_select_server () { local server_json=file # JSON containing server config local name hash local hashes=() set -- # Read names into list of positional parameters # Read hashes into "hashes" array while IFS=$'\t' read -r name hash; do set -- "$@" "$name" hashes+=( "$hash" ) done < <( jq -r '.[] | [ .name, .hash ] | @tsv' "$server_json" ) # Select wanted server local PS3='Select server: ' select SELECTED_SERVER; do [[ -n $SELECTED_SERVER ]] && break done # Get corresponding hash SELECTED_HASH=${hashes[REPLY-1]} } 
menu_select_server () { local server_json=file.json # JSON containing server config local name hash local hashes=() set -- # Read names into list of positional parameters # Read hashes into "hashes" array while IFS=$'\t' read -r name hash; do set -- "$@" "$name" hashes+=( "$hash" ) done < <( jq -r '.[] | [ .name, .hash ] | @tsv' "$server_json" ) # Select wanted server local PS3='Select server: ' select SELECTED_SERVER; do [[ -n $SELECTED_SERVER ]] && break done # Get corresponding hash SELECTED_HASH=${hashes[REPLY-1]} } 
Source Link
Kusalananda
  • 356.3k
  • 42
  • 737
  • 1.1k
Loading