0

I am running a command to filter docker images from my ECR repo.

aws ecr list-images --repository-name {name} --filter "tagStatus=TAGGED" --query 'imageIds[?imageTag=={some string}]' --output json

If no matches are found, the output is an empty array like so:

[]

If matches are found. the output looks something like this:

[
        {
            "imageDigest": "sha256:b2adff0....",
            "imageTag": "latest"
        }
]

The goal is to figure out if the tag I am querying for exists in the output.

I thought I could check check if the resulting array was empty by running if [[ ${#IMAGES[0]} == 0 ]]; then but both outputs have a length of 1.

I'm not a bash expert so any advice would be greatly appreciated.

Thanks!

Evan Lalo
  • 1,209
  • 1
  • 14
  • 34
  • 1
    You can pipe the output of the command into `jq` and perform validations and subqueries on the output. See e.g. https://stackoverflow.com/questions/21334348/how-to-count-items-in-json-object-using-command-line. There is also a `length` check. – Chris Apr 19 '22 at 14:57
  • If `$IMAGES` just contains output string, you can do `if test "$IMAGES" = "[]"` – Philippe Apr 19 '22 at 15:20

1 Answers1

0

Using jq to parse the json output you can create a bash array:

images=( $(aws ecr list-images --repository-name {name} --filter "tagStatus=TAGGED" --query 'imageIds[?imageTag=={some string}]' --output json | jq '.[].imageTag') )
printf 'number of images: %d\n' "${#images[@]}"
printf '%s\n' "${images[@]}"
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134