namerefs和管道进入bash

时间:2015-09-02 21:15:43

标签: linux bash shell bash4

当我正在使用我的函数时,我无法让bash namerefs正常工作。

我在下面有一个函数,它接受一个json blob并将其转换为一个bash关联数组。由于bash无法按值传递关联数组,因此我编写了函数来接收返回值的名称,已将declare d作为关联数组。然后它为它设置一个nameref并写入它。

第一个例子有效:

jq_to_aa () {
  # takes a json hash as $2, and the name of a var in $1. Adds all entries from $2 into $1, via jq.

  local -n j2a_result=$1
  input="${@:2}"
  input=$(echo "$input" | jq -rM ". | to_entries | .[] | @text \"\(.key) \(.value)\"")

  while read -r key val; do
    j2a_result["$key"]="$val"
  done < <(echo "${input}")
}

data='{"a": 0.96}'
declare -A assoc=( )
jq_to_aa assoc "${data}"
echo expect 0.96: ${assoc[@]}

$ bash script1.sh expect 0.96: 0.96

好的,到目前为止一切顺利。但是,我希望函数通过管道接收数据。显而易见的变化是将其称为echo "${data}" | jq_to_aa myvar,其功能如下:

jq_to_aa () {
  # takes a json hash in stdin, and the name of an var in $1. Adds all entries from stdin into $1.

  input=$(jq -rM ". | to_entries | .[] | @text \"\(.key) \(.value)\"")

  local -n j2a_result=$1
  while read -r key val; do
    j2a_result["$key"]="$val"
  done < <(echo "${input}")
}

data='{"a": 0.96}'
declare -A assoc=( )
echo "${data}" | jq_to_aa assoc
echo expect 0.96: ${assoc[@]}

$ bash script2.sh expect 0.96:

这似乎不起作用,我很难理解为什么。我怀疑是,输入值导致问题,也许是因为它创建了一个子shell(是吗?我不知道),然后值被分开。

为什么这不起作用,如何解决?

1 个答案:

答案 0 :(得分:1)

使用管道导致该函数在子shell中运行。子shell中的变量赋值不会影响父shell。请改用here-string。

jq_to_aa assoc <<<"$data"