'Bash - Check if AWS CLI command output is empty

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!



Solution 1:[1]

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[@]}"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Diego Torres Milano