Home > Net >  Escaping Bash $ with JQ
Escaping Bash $ with JQ

Time:08-05

I have a number of ec2 instances running in AWS, and I've extracted this information into a file.

aws ec2 describe-instances > instances.json

I also have another file ipAddressList

cat ipAddressList

10.100.39.4
10.100.56.20
10.100.78.11
10.100.78.12

I would like to extract the ImageId for these 4 instances.

I'm able to get the ImageId for individual ip addresses using this command

cat instances.json | jq '.Reservations[] | .Instances[] | select(.PrivateIpAddress == "10.100.39.41") | .ImageId'

But I would like to put this into a bash loop to extract the ImageId's for all 4 instances at once.

I've tried

for i in `cat ipAddressList` ; do jq '.Reservations[] | .Instances[] | select(.PrivateIpAddress == \$i) | .ImageId' instances.json ; done

But it throws an error. What am I doing wrong please?

CodePudding user response:

Don't inject data into code, use the --arg and --argjson options provided by jq to do that safely:

for i in `cat ipAddressList`
  do jq --arg i "$i" '
    .Reservations[] | .Instances[]
    | select(.PrivateIpAddress == $i) | .ImageId
  ' instances.json
done

On top of that, jq provides options to read in files as raw text, so you could shift the entire loop into jq logic, resulting in just one invocation of jq:

jq --rawfile ips ipAddressList '
  .Reservations[].Instances[]
  | select(IN(.PrivateIpAddress; ($ips / "\n")[])).ImageId
' instances.json

CodePudding user response:

You're really close with your solution.

What you need is

for i in `cat ipAddressList` ; do jq '.Reservations[] | .Instances[] | select(.PrivateIpAddress == "'$i'") | .ImageId' instances.json ; done

And you should be fine.

  • Related