【发布时间】:2021-09-20 20:39:08
【问题描述】:
在一个 shell 脚本(比如master_script.sh)中,我尝试使用jq 解析 json 文件(country.json)并将这些值传递给另一个 shell 脚本(child_script.sh),然后打印来自 @ 987654327@,因为我将在那里使用它。
master_script.sh
#!/bin/bash
$(jq -r ' .countries[] | .country as $cntry | .city[] | (.) as $ct |
"child_script.sh $cntry $ct"' country.json)
country.json
{
"countries": [
{"country":"India","city":["India1","India2","India3"]},
{"country":"USA","city":["USA1","USA2","USA3"]}
]
}
child_script.sh
#!/bin/bash
country=$1
city=$2
echo "country: $country, city: $city"\n
# I need to use these two variables for further calculation
child_script.sh 的所需输出应类似于:jqplay
country: India, city: India1
country: India, city: India2
country: India, city: India3
country: USA, city: USA1
country: USA, city: USA2
country: USA, city: USA3
但我无法做到这一点。
【问题讨论】:
-
请注意,使用命令替换来生成 shell 命令通常不会像您期望的那样工作 - 请参阅 BashFAQ #50。 (如果它确实按您预期的方式工作,那将无法在 shell 语言中处理不受信任的数据,所以它不这样做是一件非常好的事情。