[英]jq: How do I assign a jq output to a variable?
I'm new to jq and struggling to obtain a json value and use it in a bash IF statement.我是 jq 的新手,正在努力获取 json 值并在 bash IF 语句中使用它。
{
"animal": "elephant",
"colour": "red"
}
VAR=$( jq '.animal' animal.json )
echo "$VAR"
if [[ "$VAR" == "elephant" ]]; then
echo "this is an elephant"
else
echo "failed"
fi
When I run the script the comparison fails当我运行脚本时,比较失败
What can change to make the script work?什么可以改变以使脚本工作?
jq
outputs "elephant"
, note the "
jq
输出"elephant"
,注意"
Add -r
to the jq
command so the quotes are removed (raw-mode)将
-r
添加到jq
命令中,以便删除引号(原始模式)
Then the output will match the string elephant
然后 output 将匹配字符串
elephant
#!/bin/bash
VAR="$(jq -r '.animal' animal.json)"
echo "$VAR"
if [[ "$VAR" == "elephant" ]]; then
echo "this is an elephant"
else
echo "failed"
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.