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

for loop in shell script
nested for loop in shell script
while loop in shell script
for loop in shell script range
for loop in unix shell script to read a file
bash for loop variable
bash for loop one line
bash for loop list
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

 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. Then you can iterate over it instead of parsing it with 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

12 Bash For Loop Examples for Your Linux Shell Scripting, There are two types of bash for loops available. examples on how to use the bash for loop in your shell scripts. The list can be a variable that contains several words separated by spaces. It will not go inside the loop. with the error bad loop variable … after doing the change you have given , I got it� Shell Scripting: Expert Recipes for Linux, Bash and more is my 564-page book on Shell Scripting. The first half explains the features of the shell; the second half has real-world shell scripts, organised by topic, with detailed discussion of each script.

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

Bash For Loop Examples, Explains how to use a Bash for loop control flow statement on Linux / UNIX / * BSD A for loop can be used at a shell prompt or within a shell script itself. data in the shell variable, and we can use for a loop as follows to read the data: CENTOS/RHEL example (for fedora replace yum with dnf) ## for s in� There are 3 basic loop structures in Bash scripting which we'll look at below. There are also a few statements which we can use to control the loops operation. While Loops. One of the easiest loops to work with is while loops. They say, while an expression is true, keep executing these lines of code. They have the following format: while

 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.

Modify global variable in while loop, In your example the while-loop is executed in a subshell, so changes to the variable inside the while-loop won't affect the external variable. This is because� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Advanced Bash-Scripting Guide, Batch file keywords / variables / operators, and their shell equivalents; N-2. Bash scripting provides a way to explore the capabilities of these Example 2-1. cleanup: A script to clean up log files in /var/log to the next iteration of the loop, skipping all the remaining commands in that particular loop cycle. We need to export the variable for it to be inherited by another program - including a shell script. Type: $ export MYVAR $ ./ myvar2. sh MYVAR is: hello MYVAR is: hi there. Now look at line 3 of the script: this is changing the value of MYVAR. But there is no way that this will be passed back to your interactive shell. Try reading the value of

Loops, In this section of our Bash Scripting Tutorial we'll look at the different loop formats available Line 4 - We'll initialise the variable counter with it's starting value. Here echo is being used as it's an easy way to illustrate what is going on. The difference is that it will execute the commands within it until the test becomes true. Cleaning Log Files. The next simple example demonstrates a handy way we can use shell scripts in real life. This program will simply delete all log files present inside your /var/log directory. You can change the variable that holds this directory for cleaning up other logs.

JavaScript Loops Explained: For Loop, While Loop, Dowhile Loop , Variables created here are scoped to the loop. way of getting an object's keys using an expression within the for-in loop's Setup var myArray = []; // Only change code below this line. var i = 0; while (i Cat Command in Linux CSS Background Image HTML Background Color CSS Comment Example. Note that the check expression cannot be simplified or internally optimised by the interpreter because the value may change inside the loop’s body (yes, there are cases where you’d want to do this, however rare and stupid they may seem), hence the variables are volatile and get re-evaluted.

Comments
  • I can't choose to create an array because these are parameters from a web app that create multiple field selection with a comma separeted value.
  • ./array.sh: 2: ./array.sh: Syntax error: "(" unexpected
  • sh or bash? sh not support arrays, you need bash #!/bin/bash