bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

mapfile associative array shim Was: Re: [feature request] add associativ


From: Léa Gris
Subject: mapfile associative array shim Was: Re: [feature request] add associative array support to mapfile
Date: Thu, 17 Dec 2020 16:22:38 +0100
User-agent: Telnet/1.0 [tlh] (PDP11/DEC)

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

[Prev in Thread] Current Thread [Next in Thread]