arrays - 如何存储 for 循环结果并在命令中引用?
问题描述
我有一个如下所示的 for 循环,
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
现在,我希望在下面的命令中引用每个结果:
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}"...
任何人都可以帮助我吗?
解决方案
添加一些换行符确实有助于提高代码的可读性。
利用 shell 数组:
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[@]}"
推荐阅读
- vba - InsertMenuItem 用于在文本框中弹出
- python - Dill: After import, unpickling yields segmentation fault; what possible things could be going wrong?
- java - 在模块 jetified-commons-codec-1.15 (commons-codec:commons-codec:1.15) 中发现重复的类 org.apache.commons.codec.BinaryDecoder
- javascript - 如何获取一个“值”属性html中的元素?
- vue.js - Vue 将数据同步回父级
- typescript - Typescript 将 OOP 代码替换为功能代码
- azure - 如何强制执行 Azure 应用服务计划策略以限制应用服务计划中托管的 webapp 总数
- python - 尝试使用 multipart/form-data 时,无服务器 Flask API 崩溃
- python - 没有 \n 的 Python 打印
- reactjs - 字符串 var 的条件渲染