带有 jq 和 grep 的多个 if 条件不正确匹配,并且 returns true 根本不应该匹配
Multiple `if` conditions with `jq` and `grep` incorrectly matches and returns true when it shouldn't match at all
我有一个 json
文件:
cat myjsonfile.json
{
"directory": true,
"condition": "",
"specialCondition": "",
"dataFiles": "",
"nonstandard-protocol": true
}
specialCondition
是标准化的,可以为空或具有 matched
或 unmatched
状态。
我只是想 translate
当 nonstandard-protocol
是 false
时的那些条件到另一个状态。
所以我在 bash 脚本中写了这个。
if [[ "jq '.specialCondition' myjsonfile.json | grep -q 'matched'" && "jq '."nonstandard-protocol"' myjsonfile.json | grep -q 'false'" ]]; then echo 'MATCHED' | cat > protocol_result.txt; fi
if [[ "jq '.specialCondition' myjsonfile.json | grep -q 'unmatched'" && "jq '."nonstandard-protocol"' myjsonfile.json | grep -q 'false'" ]]; then echo 'NOTMATCHED' | cat > protocol_result.txt; fi
然而,这个returns不正确的结果。当我 运行 脚本时,我总是看到它首先将 MATCHED
写入我的 protocol_result.txt
,然后在第二行 if
中将 NOTMATCHED
写入文件!虽然它根本不应该写任何东西......为什么会这样?
将 if
语句带到 jq
并让它输出任何你想要的。
如果 nonstandard-protocol
为真,或者 specialCondition
既不是 matched
也不是 unmatched
,则以下示例不打印任何内容 ""
。否则它会打印 MATCHED
或 NOTMATCHED
,这取决于 specialCondition
:
的内容
jq --raw-output '
if ."nonstandard-protocol" then ""
else if .specialCondition == "matched" then "MATCHED"
elif .specialCondition == "unmatched" then "NOTMATCHED"
else "" end
end
' myjsonfile.json > protocol_result.txt
注意:使用 ""
将不会像预期的那样打印任何内容,但会在后面跟一个换行符,因为它有一个输出(那么它实际上是一个空行)。如果您不希望这样,请将 ""
更改为 empty
。
您需要实际执行您的 grep
命令,如果您需要它的状态代码:
if jq .specialCondition myjsonfile.json | tee protocol_result.txt | grep -q matched && jq '."nonstandard-protocol"' myjsonfile.json | tee -a protocol_result.txt | grep -q false
then
.....
fi
我有一个 json
文件:
cat myjsonfile.json
{
"directory": true,
"condition": "",
"specialCondition": "",
"dataFiles": "",
"nonstandard-protocol": true
}
specialCondition
是标准化的,可以为空或具有 matched
或 unmatched
状态。
我只是想 translate
当 nonstandard-protocol
是 false
时的那些条件到另一个状态。
所以我在 bash 脚本中写了这个。
if [[ "jq '.specialCondition' myjsonfile.json | grep -q 'matched'" && "jq '."nonstandard-protocol"' myjsonfile.json | grep -q 'false'" ]]; then echo 'MATCHED' | cat > protocol_result.txt; fi
if [[ "jq '.specialCondition' myjsonfile.json | grep -q 'unmatched'" && "jq '."nonstandard-protocol"' myjsonfile.json | grep -q 'false'" ]]; then echo 'NOTMATCHED' | cat > protocol_result.txt; fi
然而,这个returns不正确的结果。当我 运行 脚本时,我总是看到它首先将 MATCHED
写入我的 protocol_result.txt
,然后在第二行 if
中将 NOTMATCHED
写入文件!虽然它根本不应该写任何东西......为什么会这样?
将 if
语句带到 jq
并让它输出任何你想要的。
如果 nonstandard-protocol
为真,或者 specialCondition
既不是 matched
也不是 unmatched
,则以下示例不打印任何内容 ""
。否则它会打印 MATCHED
或 NOTMATCHED
,这取决于 specialCondition
:
jq --raw-output '
if ."nonstandard-protocol" then ""
else if .specialCondition == "matched" then "MATCHED"
elif .specialCondition == "unmatched" then "NOTMATCHED"
else "" end
end
' myjsonfile.json > protocol_result.txt
注意:使用 ""
将不会像预期的那样打印任何内容,但会在后面跟一个换行符,因为它有一个输出(那么它实际上是一个空行)。如果您不希望这样,请将 ""
更改为 empty
。
您需要实际执行您的 grep
命令,如果您需要它的状态代码:
if jq .specialCondition myjsonfile.json | tee protocol_result.txt | grep -q matched && jq '."nonstandard-protocol"' myjsonfile.json | tee -a protocol_result.txt | grep -q false
then
.....
fi