【发布时间】:2019-04-14 02:37:39
【问题描述】:
我正在尝试编写一个从诺贝尔奖 JSON 文件中解析的 bash 脚本。第一个参数是年份,第二个参数是类别。
set -- 2000 physics # set $1 to 2000 and $2 to physics
jq -cr --argjson y "$1" --arg c "$2" \
'.prizes[] | select(.category == $c)| select(.year == $y )' <<'EOF'
{"prizes": [
{"category": "math", "year": "1999"},
{"category": "physics", "year": "2000"}]}
EOF
...没有输出,而它应该用
输出一行{"category":"physics","year":"2000"}
但是,只有类别来自变量,它才有效:
set -- physics # set $1 to physics
jq -cr --arg c "$1" \
'.prizes[] | select(.category == $c)| select(.year == "2000" )' <<'EOF'
{"prizes": [
{"category": "math", "year": "1999"},
{"category": "physics", "year": "2000"}]}
EOF
...正确地发出上面给出的行。
因此,只有数字年份无法通过 - 无论问题是什么,它都不适用于该类别。
【问题讨论】:
-
当你说
$YEAR被“链接”到$1你的意思是你的脚本中有YEAR=$1吗?为什么第一个例子用$CATEGORY,第二个例子用$category? -
我在问题中编辑了minimal reproducible example,代码有人可以复制和粘贴运行以查看问题,无需任何更改或未提供的输入文件,并给出准确的预期输出。以后请尝试自己做。