带单引号的 jq bash 脚本
jq with single quotes bash script
我尝试使用 bash 来 运行 以下脚本:
#!/bin/bash
myvar="data1"
data='[
{
"resource_name": "data1.something",
"resource_type": "Topic"
},
{
"resource_name": "data2.something",
"resource_type": "Topic"
}
]'
query=$(echo ".[] | select((.resource_type==\"Topic\") and (.resource_name | startswith(\"${myvar}\") | not))")
echo ${data} | jq ${query}
它不起作用,因为行:
echo ${data} | jq ${query}
但是如果我 运行 在 zsh 中使用相同的脚本,它就可以工作。并给我以下错误:
jq: error: Could not open file |: No such file or directory
jq: error: Could not open file select((.resource_type=="Topic"): No such file or directory
jq: error: Could not open file and: No such file or directory
jq: error: Could not open file (.resource_name: No such file or directory
jq: error: Could not open file |: No such file or directory
jq: error: Could not open file startswith("data1"): No such file or directory
jq: error: Could not open file |: No such file or directory
jq: error: Could not open file not)): No such file or directory
我无法理解这里到底是什么问题,我只能认为在与 bash.
一起使用时我需要以某种方式添加单引号
例如,如果我使用单引号:
echo ${data} | jq \'${query}\'
报错:
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
'.[]
jq: 1 编译错误
使用 --arg
选项将内容作为变量导入优于将 shell 变量注入实际过滤器代码。这也使您免于处理单引号或双引号。
#!/bin/bash
myvar="data1"
data='[
{
"resource_name": "data1.something",
"resource_type": "Topic"
},
{
"resource_name": "data2.something",
"resource_type": "Topic"
}
]'
query='.[] | select((.resource_type==$type) and (.resource_name | startswith($var) | not))'
echo "${data}" | jq --arg type "Topic" --arg var "${myvar}" "${query}"
{
"resource_name": "data2.something",
"resource_type": "Topic"
}
我尝试使用 bash 来 运行 以下脚本:
#!/bin/bash
myvar="data1"
data='[
{
"resource_name": "data1.something",
"resource_type": "Topic"
},
{
"resource_name": "data2.something",
"resource_type": "Topic"
}
]'
query=$(echo ".[] | select((.resource_type==\"Topic\") and (.resource_name | startswith(\"${myvar}\") | not))")
echo ${data} | jq ${query}
它不起作用,因为行:
echo ${data} | jq ${query}
但是如果我 运行 在 zsh 中使用相同的脚本,它就可以工作。并给我以下错误:
jq: error: Could not open file |: No such file or directory
jq: error: Could not open file select((.resource_type=="Topic"): No such file or directory
jq: error: Could not open file and: No such file or directory
jq: error: Could not open file (.resource_name: No such file or directory
jq: error: Could not open file |: No such file or directory
jq: error: Could not open file startswith("data1"): No such file or directory
jq: error: Could not open file |: No such file or directory
jq: error: Could not open file not)): No such file or directory
我无法理解这里到底是什么问题,我只能认为在与 bash.
一起使用时我需要以某种方式添加单引号例如,如果我使用单引号:
echo ${data} | jq \'${query}\'
报错:
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
'.[] jq: 1 编译错误
使用 --arg
选项将内容作为变量导入优于将 shell 变量注入实际过滤器代码。这也使您免于处理单引号或双引号。
#!/bin/bash
myvar="data1"
data='[
{
"resource_name": "data1.something",
"resource_type": "Topic"
},
{
"resource_name": "data2.something",
"resource_type": "Topic"
}
]'
query='.[] | select((.resource_type==$type) and (.resource_name | startswith($var) | not))'
echo "${data}" | jq --arg type "Topic" --arg var "${myvar}" "${query}"
{
"resource_name": "data2.something",
"resource_type": "Topic"
}