<p>Look this: </p>

<pre><code>#!/bin/bash
#Get servers list
set -f
string=$DEPLOY_SERVER
array=(${string//,/ })
for i in "${!array[@]}"do    
      echo "Deploy project on server ${array[i]}"    
done
</code></pre>

<p>I found this <a href="https://medium.com/@lucabecchetti/autodeploy-from-gitlab-to-multiple-aws-ec2-instances-a43448727c5a" rel="nofollow noreferrer">link</a></p>

CC BY-SA 4.0
2018-12-12T15:47:03.650Z
2018-12-12T15:47:03.650Z
dalmo.santos
<p>An elegant way is to use <strong>IFS environment variable</strong> (Internal Field Separator) to define a <em>delimiter</em> when assigning a string list to array.</p>
<pre class="lang-bash prettyprint-override"><code>IFS=&quot;,&quot; SERVERS=($SERVERS) #BTW, no need to declare a new variable, just reassign

for SERVER in ${SERVERS[@]} 
do 
   echo $SERVER
done
</code></pre>

CC BY-SA 4.0
2021-02-09T11:35:56.463Z
2021-02-09T11:42:28.840Z
2021-02-09T11:42:28.840Z
bebyx
<p>I have configured jenkins parameter deployment job using active choices parameterize plugin. In my dev server section have two servers called, 192.168.94.139, 192.168.94.140</p>

<p><a href="https://i.sstatic.net/Ble8u.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Ble8u.png" alt="array"></a> </p>

<p>I want to read this array input and deploy to both servers. As an example if I select both servers artifacts should be deployed to both servers. How can I read those inputs to fulfill my requirement. Until this point I wrote an array to read inputs but it prints like this </p>

<blockquote>
  <p>192.168.94.139,192.168.94.140</p>
</blockquote>

<pre><code>my_array=($SERVER_SELECTION)
for i in ${my_array[@]} 
do 

   echo $my_array

done
</code></pre>

<jenkins><jenkins-plugins><shell-script>
How to read array and loop server selection and deployment
Janith
# How to read array and loop server selection and deployment

(Asked by: username_0 on 2018-12-05)

I have configured jenkins parameter deployment job using active choices parameterize plugin. In my dev server section have two servers called, 192.168.94.139, 192.168.94.140
 
I want to read this array input and deploy to both servers. As an example if I select both servers artifacts should be deployed to both servers. How can I read those inputs to fulfill my requirement. Until this point I wrote an array to read inputs but it prints like this 

192.168.94.139,192.168.94.140

my_array=($SERVER_SELECTION)
for i in ${my_array[@]} 
do 

   echo $my_array

done

**Tags:** jenkins, jenkins-plugins, shell-script


---

## 2 answers

### Answer by username_1 on 2018-12-12. Score: 2

Look this: 
#!/bin/bash
#Get servers list
set -f
string=$DEPLOY_SERVER
array=(${string//,/ })
for i in "${!array[@]}"do    
      echo "Deploy project on server ${array[i]}"    
done

I found this link


---

### Answer by username_2 on 2021-02-09. Score: 0

An elegant way is to use IFS environment variable (Internal Field Separator) to define a delimiter when assigning a string list to array.
IFS="," SERVERS=($SERVERS) #BTW, no need to declare a new variable, just reassign

for SERVER in ${SERVERS[@]} 
do 
   echo $SERVER
done


---

