【发布时间】:2021-10-03 17:28:32
【问题描述】:
我正在尝试找到一种干净的方法来将 2 个 json 字符串值分配给 while 循环中的 2 个变量。我正在使用的循环和输入如下。
输入:
foo='
[
{
"name":"name string (more info)",
"nested_name":{
"name":"my name",
"conclusion":"failure",
"number":11
}
},
{
"name":"name string (more info)",
"nested_name":{
"name":"my other name",
"conclusion":"failure",
"number":13
}
}
]'
目前我有以下:
echo "$foo" | jq ".[].name,.[].nested_name.name" | while read -r foo bar; do
# do stuff with foo and bar
done
在第 1 次迭代中,我想要:
foo = "name string (more info)"
bar = "my name"
迭代 2:
foo = "name string (more info)"
bar = "my other name"
但是这会产生以下错误输出:
foo = "name
bar = string (more info)"
foo = "name
bar = string (more info)"
foo = "my
bar = name"
foo = "my
bar = other name"
我一整天都在这,任何意见或建议将不胜感激!
【问题讨论】:
标签: json bash while-loop jq ifs