[英]Bash - Check if AWS CLI command output is empty
我正在運行一個命令來從我的 ECR 存儲庫中過濾 docker 個圖像。
aws ecr list-images --repository-name {name} --filter "tagStatus=TAGGED" --query 'imageIds[?imageTag==
{some string} ]' --output json
如果未找到匹配項,則 output 是一個空數組,如下所示:
[]
如果找到匹配項。 output 看起來像這樣:
[
{
"imageDigest": "sha256:b2adff0....",
"imageTag": "latest"
}
]
目標是弄清楚我要查詢的標簽是否存在於 output 中。
我想我可以通過運行if [[ ${#IMAGES[0]} == 0 ]]; then
if [[ ${#IMAGES[0]} == 0 ]]; then
,但兩個輸出的長度均為 1。
我不是 bash 專家,所以任何建議將不勝感激。
謝謝!
使用jq
解析 json output 可以創建一個 bash 數組:
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[@]}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.