带有 bash 变量的 JMESPath 查询表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33131261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JMESPath query expression with bash variable
提问by TomH
Messing around with a simple aws cli query to check for the existence of a Lambda function and echo the associated role if it exists:
使用一个简单的 aws cli 查询来检查 Lambda 函数是否存在,如果存在则回显关联的角色:
#!/bin/bash
fname=
role=$(aws lambda list-functions --query 'Functions[?FunctionName == `$fname`].Role' --output text)
echo "$fname role: $role"
However, $fname appears to be resolving to an empty string in the aws command. I've tried escaping the back ticks, swapping ` to ' and a miriad of other thrashing edits (and yes, I'm passing a string on the cl when invoking the script :)
但是, $fname 似乎在 aws 命令中解析为空字符串。我试过转义后记号,交换 ` to ' 和大量其他颠簸编辑(是的,我在调用脚本时在 cl 上传递一个字符串:)
How do I properly pass a variable into JMESPath query inside a bash script?
如何在 bash 脚本中将变量正确传递到 JMESPath 查询中?
回答by jamesls
Because the whole JMESPath expression is enclosed in single quotes, bash is not expanding the $fname
variable. To fix this you can surround the value with double quotes and then use single quotes (raw string literals) for the $fname
var:
因为整个 JMESPath 表达式都用单引号括起来,所以 bash 不会扩展$fname
变量。要解决此问题,您可以用双引号将值括起来,然后对var使用单引号(原始字符串文字)$fname
:
aws lambda list-functions --query "Functions[?FunctionName == '$fname'].Role" --output text
回答by jobwat
Swapping the backticks to single quotes, didn't work for me... :(
将反引号交换为单引号,对我不起作用...... :(
But escaping the backticks works :)
但是逃避反引号是有效的:)
Here are my outputs:
这是我的输出:
aws elbv2 describe-listeners --load-balancer-arn $ELB_ARN --query "Listeners[?Port == '$PORT'].DefaultActions[].TargetGroupArn | [0]"
null
空值
aws elbv2 describe-listeners --load-balancer-arn $ELB_ARN --query "Listeners[?Port == \`$PORT\`].DefaultActions[].TargetGroupArn | [0]"
"arn:aws:elasticloadbalancing:ap-southeast-2:1234567:targetgroup/xxx"
“arn:aws:elasticloadbalancing:ap-southeast-2:1234567:targetgroup/xxx”