How to store for loop result and refer in a command?

Viewed 66

I have a for loop like below,

values="addressSearchBaseUrl addressSearchSubscriptionKey cacheUrl calendarApiUrl checkoutBffApiUrl cpCode"

for ptr in $values
do
echo $ptr
temp=$(az pipelines variable-group list --group-name "${target_backend}"|jq '.[0].variables.'${ptr}'.value')
echo $temp
echo $?
done

Now, I want each result to be referred in the command below:

az pipelines variable-group variable create true  --name "Sales.Configuration.Spa ${new_env}" --variable "addressSearchBaseUrl" --value "${parse or store the value from above loop}" "addressSearchSubscriptionKey" "--value "${parse or store the value from above loop}"...

can anyone kindly assist me please?

1 Answers

Adding a few newlines can really help with the readability of your code.

Take advantage of shell arrays:

values=(
    addressSearchBaseUrl
    addressSearchSubscriptionKey
    cacheUrl
    calendarApiUrl
    checkoutBffApiUrl
    cpCode
)
az_create_options=()

for ptr in "${values[@]}"
do
    result=$(
        az pipelines variable-group list --group-name "${target_backend}" \
        | jq ".[0].variables.${ptr}.value"
    )
    printf "%s\t%s\t%d\n" "$ptr" "$result" $?

    # add the variable and value to the array
    az_create_options+=( --variable "$ptr" --value "$result" )
done

# inspect the create options, if you want
declare -p az_create_options

# now, create them
az pipelines variable-group variable create true  \
    --name "Sales.Configuration.Spa ${new_env}" \
    "${az_create_options[@]}"
Related