在 jq 命令中使用管道符号会引发错误

问题描述 投票:0回答:1

我有一个shell脚本文件

validation-example.sh
,它有以下内容

expected_template = '{
  "remoteIds": [
    {
      "remoteId": "",
      "requestsReceived": 1
    },
    {
      "remoteId": "",
      "requestsReceived": 1
    },
    {
      "remoteId": "",
      "requestsReceived": 1
    },
    {
      "remoteId": "",
      "requestsReceived": 1
    }
  ]
}'

actual_json = '{
  "remoteIds": [
    {
      "remoteId": "[fd00:10:244:1:0:0:0:12]--(http://fd00-10-244-1--12.test.pod:8080)",
      "requestsReceived": 1
    },
    {
      "remoteId": "[fd00:10:244:1:0:0:0:12]--(http://fd00-10-244-1--12.test.pod:8081)",
      "requestsReceived": 1
    },
    {
      "remoteId": "[fd00:10:244:1:0:0:0:12]--(http://fd00-10-244-1--12.test.pod:8082)",
      "requestsReceived": 1
    },
    {
      "remoteId": "[fd00:10:244:1:0:0:0:12]--(http://fd00-10-244-1--12.test.pod:8083)",
      "requestsReceived": 1
    }
  ]
}'

validate_json() {
    local data="$1"
    local template="$2"
    jq -e "$data | $template" >/dev/null
}


if validate_json "$actual_json" "$expected_template"; then
    echo "The actual JSON matches the expected template."
else
    echo "The actual JSON does not match the expected template."
fi

但是,当我运行该文件时,它返回以下错误

validation-example.sh: line 1: expected_template: command not found
validation-example.sh: line 22: actual_json: command not found
jq: error: syntax error, unexpected '|', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
 |  
jq: 1 compile error
The actual JSON does not match the expected template.

请帮忙 谢谢

json sh jq
1个回答
0
投票

删除

expected_template
actual_json
作业周围的空白。编辑为:

actual_json='{

expected_template='{

在这里了解更多表格

© www.soinside.com 2019 - 2024. All rights reserved.