这是我的脚本,应该比较 2 个文件,但参数来自第三个 txt 文件
diff_output=$(diff <(jq ".$param" "$path_to_file1") <(jq ".$param" "$path_to_file2"))
在我的txt文件中
.txt
args1,args2,def.args-pod,def.car
args1、args2、def.car 可以工作,但 def.args-pod 不行并发送答案
jq: error: pod/0 is not defined at <top-level>, line 1:
请帮我改变这个 diff_output=$(差异 <(jq ".$param" "$path_to_file1") <(jq ".$param" "$path_to_file2"))
我的yaml
{
"args1": "/my-api/",
"def": {
"args-pod": "^0.7.9",
"car": "^0.8.8"},
"args2": "^0.9.5",
}
假设“$param”的值为“def.args-pod”,当插入到 jq 的调用中时将不起作用。
jq ".$param" "$file"
会变成 jq '.def.args-pod' '/path/to/file'
这显然是不正确的。为了使程序正确,这需要是 ."def.args-pod"
或 .["def.args-pod"]
。
但是无论如何你都不应该使用字符串插值来构建 jq 程序,而是传递变量并使用它们:
jq --arg prop "$param" '.[$prop]' "$file"
假设您的输入 JSON 如下所示:
{
"def.args-pod": "some value, array, or nested object"
}
如果没有,请编辑您的答案以提供最小的、可重现的示例。
延伸阅读:变量有保留字符时如何使用jq?