简体   繁体   English

有没有办法在 shell 脚本中更改/清除 for cicle 循环内的变量?

[英]Is there a way to change/clean a variable inside a for cicle loop in a shell script?

RD_OPTION_AZWEBAPPNAME="01-SM1,02-SM1Touch,03-Data"
for i in $(echo $RD_OPTION_AZWEBAPPNAME | sed "s/,/ /g");
    do
          /bin/az group deployment create --name Template2020 --RD_OPTION_AZWEBAPPNAME=$i

With this command I create 3 APP with 01-SM1 02-SM1Touch, 03-Data but I need to insert a piece of this array in another parameter in order to have SM1 SM1Touch Data withot the number and the "-" before the APP name INSIDE a for cicle, like below使用此命令,我使用 01-SM1 02-SM1Touch, 03-Data 创建了 3 个 APP,但我需要在另一个参数中插入此数组的一部分,以便 SM1 SM1Touch 数据在 APP 名称前不带数字和“-” INSIDE a for cicle,如下图

 RD_OPTION_AZWEBAPPNAME="01-SM1,02-SM1Touch,03-Data"
for i in $(echo $RD_OPTION_AZWEBAPPNAME | sed "s/,/ /g");
    do
          /bin/az group deployment create --name Template2020 --RD_OPTION_AZWEBAPPNAME=$i --webappconf=$WEBAPPNAMEWITHOUTNUMBERANDMINUSBEFORE

You should use an array for RD_OPTION_AZWEBAPPNAME instead of a string.您应该为RD_OPTION_AZWEBAPPNAME使用数组而不是字符串。 Then you can iterate over it instead of parsing it with sed .然后你可以迭代它而不是用sed解析它。

Something like this :像这样的事情:

RD_OPTIONS=(
"SM1"
"SM1Touch"
"Data"
)

for number in `seq -f "%02g" 1 ${#RD_OPTIONS[@]}`
do
    name=${RD_OPTIONS[$number-1]}
    full="$number-$name"
    echo "number: $number"
    echo "name: $name"
    echo "full: $full"
done

will print将打印

number: 01
name: SM1
full: 01-SM1
number: 02
name: SM1Touch
full: 02-SM1Touch
number: 03
name: Data
full: 03-Data

So you could do this :所以你可以这样做:

RD_OPTIONS=(
"SM1"
"SM1Touch"
"Data"
)

for number in `seq -f "%02g" 1 ${#RD_OPTIONS[@]}`
do
    name=${RD_OPTIONS[$number-1]}
    full="$number-$name"
    /bin/az group deployment create --name Template2020 --RD_OPTION_AZWEBAPPNAME=$full --webappconf=$name
done

Consider this考虑这个

RD_OPTION_AZWEBAPPNAME="01-SM1,02-SM1Touch,03-Data"
arr1=( ${RD_OPTION_AZWEBAPPNAME//,/' '} ) # conver your var to an array
arr2=( ${arr1[@]//*-/} )                  # create second array witn names SM1, SM1Touch, Data
arr3=( ${arr1[@]} ${arr2[@]} )            # create mega) array
for name in ${arr3[@]}; { your_code; }    # loop through mega array with your code
 for i in $(echo $RD_OPTION_AZWEBAPPNAME | sed "s/,/ /g");
 do
    echo $i
    export AZWEBAPPNAMENONUMBER=`echo "$i" | cut -c 4-`

This is the way I decided.这是我决定的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM