我有一个简单的bash文件,如下所示
#!/bin/bash
net=$(curl -s -H "Content-Type: application/json" -H "X-Auth-Token: $token" -d '{"network": {"name": "net1"}}' http://10.1.10.146:18090/network/v2.0/networks 2>&1 | awk '/id/{print $1}' | jq -r .network.id)
echo $net
运行此文件会给我一个错误,如下所示
parse error: Invalid numeric literal at line 2, column 0
尝试根据这些链接进行更改 https://unix.stackexchange.com/questions/354943/setting-jq-output-to-a-bash-variable
但没有任何帮助我,无法弄清楚我哪里出错了。让我知道错误和可能的更改的原因。
命令的卷曲输出
curl -s -H "Content-Type: application/json" -H "X-Auth-Token: $token" -d '{"network": {"name": "net1"}}' http://10.1.10.146:18090/network/v2.0/networks
输出:
{"network":{"status":"ACTIVE","router:external":false,"availability_zone_hints":[],"availability_zones":[],"description":"","subnets":[],"shared":false,"tenant_id":"d0e75710820c401db3291ac6278f326f","created_at":"2018-05-15T07:37:42Z","tags":[],"ipv6_address_scope":null,"mtu":1450,"updated_at":"2018-05-15T07:37:42Z","admin_state_up":true,"revision_number":2,"ipv4_address_scope":null,"is_default":false,"port_security_enabled":true,"project_id":"d0e75710820c401db3291ac6278f326f","id":"1548df56-a35b-4232-9550-54a3c2266d60","name":"net1"}}
这个想法是只从输出中获取id并存储到bash变量中,以获取我使用下面命令的id
curl -s -H "Content-Type: application/json" -H "X-Auth-Token: $token" -d '{"network": {"name": "net1"}}' http://10.1.10.146:18090/network/v2.0/networks 2>&1 | awk '/id/{print $1}' | jq -r .network.id
输出:
be831582-90c1-499c-875f-9c0b0d1969a6
我也尝试删除awk并解析curl json响应,同样的错误出现了。
提前谢谢。答案 0 :(得分:0)
出现“解析错误”消息,因为2>&1
将任何STDERR消息重定向到jq
,无法解析它。比较这些命令的输出:
> curl http://no.such.host/network/v2.0/networks 2>&1 curl: (6) Could not resolve host: no.such.host > curl http://no.such.host/network/v2.0/networks 2>&1 | jq '.' parse error: Invalid numeric literal at line 1, column 4
以下是一些想法:
set -x
以“调试”所有内容
命令。2>&1
,因为它没有帮助您!如果您提供了卷曲输出,我们可以提供更多帮助。 (我们没有对10.1.10.146主机的HTTP访问权限。)