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!

2
  • 1
    You can pipe the output of the command into jq and perform validations and subqueries on the output. See e.g. stackoverflow.com/questions/21334348/…. There is also a length check. Commented Apr 19, 2022 at 14:57
  • If $IMAGES just contains output string, you can do if test "$IMAGES" = "[]" Commented Apr 19, 2022 at 15:20

1 Answer 1

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[@]}"
Sign up to request clarification or add additional context in comments.

1 Comment

Use -r to tell jq to output the raw values, not encoded as JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.