我正在使用awscli并尝试从查询的输出中获取 IpAddress 的值。
我尝试使用 jq ,但出现编译错误。
就是这种情况:
output="$(aws efs describe-mount-targets --file-system-id fs-089b5e31)"
echo $output
{ "MountTargets": [ { "MountTargetId": "fsmt-bb29e666", "IpAddress": "172.20.33.255", "OwnerId": "668225551666", "SubnetId": "subnet-0b61377039d31e666", "NetworkInterfaceId": "eni-045f6ea1376662bdf", "FileSystemId": "fs-089b5e66", "LifeCycleState": "available" } ] }
这是我用来获取IpAddress的命令:
echo array | jq '.[]MountTarget[]s.IpAddress'
我得到的错误是这样的:
parse error: Invalid numeric literal at line 2, column 0
ubuntu@ip-10-10-16-245:~/infra-devops/kops/vector$ echo array | jq '.[]MountTarget[]s.IpAddress'
jq: error: syntax error, unexpected IDENT, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.[]MountTarget[]s.IpAddress
jq: 1 compile error
我的查询是问题所在,还是我最好改用sed?
最佳答案
您访问数组的语法错误。要获取IP地址,请使用以下命令:
aws efs describe-mount-targets --file-system-id fs-089b5e31 |
jq '.MountTargets[0].IpAddress'
MountTargets
是您要从中获得第一个对象的数组。如果需要原始数据(不带双引号),请在
-r
命令中使用jq
选项。
关于sed - 从json参数获取值不起作用(jq : 1 compile error),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51055912/