0

我是 shell 脚本的新手。我正在尝试使用

az sig image-version list

command from azure which should return a list of versions and storing it into a list/array. So I can step through the list in a for loop.

VERSIONS_LIST="$(az sig image-version list --gallery-image-definition $GALLERY_IMAGE_NAME --gallery-name $GALLERY_NAME --resource-group $RESOURCE_GROUP_NAME)`"

However, I am not sure if the command returns more than just the versions. If so how can I only take part of the output?

I am also having issue with displaying the populated list. I believe my syntax of using the azure cli to store in the list is wrong. any guidance is much appreciated.

echo VERSION_LIST

我是否将列表正确存储到变量中?

4

1 回答 1

1

您可以使用全局参数 --query并使用JMESPath从输出中--output查询版本列表,然后您可以将输出存储为 bash 中的变量,就像这样不带双引号,az sig image-version list

VERSIONS_LIST=$(az sig image-version list --gallery-image-definition $GALLERY_IMAGE_NAME --gallery-name $GALLERY_NAME --resource-group $RESOURCE_GROUP_NAME --query "xxx" --output tsv)

然后你可以用 command 检查变量echo $VERSIONS_LIST。如果你想运行 for 循环,你可以这样做,

for version in $VERSIONS_LIST
do
    echo $version

done

例如,这是一个带有 CLI 2.0 的 bash 脚本。有关更多详细信息,请参阅此博客

#!/bin/bash
rgName=nancytest
vmlist=$(az vm list -g $rgName --query "[].name" -o tsv)

for vm in  $vmlist
do

vmLocation=$(az vm show -g $rgName -n $vm --query "location" -o tsv)
echo $vm,$vmLocation

done

有关循环示例,请参见bash 。

于 2020-07-23T02:44:25.800 回答