Here is an include shim to enable associative array support to mapfile or fall-back to regular mapfile if it has that support already:
#!/usr/bin/env bash
# mapfile_assoc_shim.bash
! { mapfile -A a </dev/null;} 2>/dev/null && {
  mapfile () {
    local k v d=$'\n'
    local -n A=${*: -1:1}
    [[ ${*: -2:1} = -A ]] && {
      [[ ${*:1:1} = -d ]] && d=${*:2:1}
      while read -r -d "$d" k v || [[ -n $k && -n $v ]]
        do A[$k]=$v
      done
      true
    } || command mapfile "$@"
  }
}

Example usage:

. mapfile_assoc_shim.bash

IFS='=' mapfile -d '' -A assoc_null < <(kv_null_stream)

--
Léa Gris


Reply via email to