将 tee 输出重定向到变量而不是文件

问题描述 投票:0回答:2

我正在尝试执行循环,其中返回 json 数据的每个curl 命令的输出分配给变量/数组。我正在使用 tee 命令来获取我想要的值以及下一个 json 数据的下一个标记。当我将数据保存到文件时,一切正常,但我不想将数据保存到文件,而是将其保留在数组/变量中。

这有效:

while  [[ "$next_token" != "null" ]]

do

curl -u "$user:$password" "https://mylink.dot" | tee >(jq -r '.dataSummaries[] | .dataId' >> my_data_file.txt ) >(jq -r '.nextToken'>next_token_file.txt) > /dev/null

next_token=$(cat next_token_file.txt)

done

我试过这个:

while  [[ "$next_token" != "null" ]]

do
index=$((index+1))

curl -u "$user:$password" "https://mylink.dot" | tee >(jq -r '.dataSummaries[] | .dataId' | read my_array[index] ) >(jq -r '.nextToken'>next_token_file.txt) > /dev/null

next_token=$(cat next_token_file.txt)

done

当我使用

|read var
时,我的变量为空。

在这种情况下如何将 json 数据(和下一个标记)存储在变量中,而不是文件中。

bash curl jq tee
2个回答
3
投票

第二个

jq
应从
tee
的标准输出读取,然后写入标准输出。然后将整个管道放入另一个进程中以替换
read
进行读取。

read next_token <( curl -u "$user:$password" "https://mylink.dot" |
                     tee >(jq -r '.dataSummaries[] | .dataId' >> my_data_file.txt ) |
                     jq -r '.nextToken')

1
投票

我计划使用这种机制来获取两个值作为变量。

read var1 var2 <<<$(curl -s "http://example.com"| tee >(head  -n1) >(tail -n1) >/dev/null)
© www.soinside.com 2019 - 2024. All rights reserved.