Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMESPath query expression with bash variable

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:

#!/bin/bash

fname=$1
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 :)

How do I properly pass a variable into JMESPath query inside a bash script?

like image 607
TomH Avatar asked Oct 14 '15 16:10

TomH


2 Answers

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:

aws lambda list-functions --query "Functions[?FunctionName == '$fname'].Role" --output text
like image 165
jamesls Avatar answered Oct 24 '22 09:10

jamesls


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"

like image 23
jobwat Avatar answered Oct 24 '22 09:10

jobwat