Weisheng Wu
Weisheng Wu

Reputation: 1391

Bash Script with Azure CLI

I am new to shell scripting. I am trying to use

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

Am I storing the list correctly into the variable?

Upvotes: 1

Views: 5839

Answers (1)

Nancy Xiong
Nancy Xiong

Reputation: 28294

You can use Global Parameters --query and --output to query the version list with JMESPath from the output of az sig image-version list then you can store the output as a variable in bash like this without double quotation marks,

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)

Then you can check the variable with command echo $VERSIONS_LIST. If you want to run for loop, you may do it like this,

for version in $VERSIONS_LIST
do
    echo $version

done

For example, here is a bash script with CLI 2.0. See this blog for more details.

#!/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

See bash for loop examples.

Upvotes: 3

Related Questions