Here’s a basic loop in Bash:
1 2 3 4 5 6 |
SERVERS="s1 s2 s3 s4" for S in $SERVERS do echo "Server: $S" # ... do something ... done |
Notice that it iterates over a space delimited string of values.
If you have a comma delimited list then you can do something like this:
1 2 3 4 5 6 7 8 9 |
SERVERS="s1,s2,s3,s4" OLDIFS=$IFS IFS=',' for S in $SERVERS do echo "Server: $S" # ... do something ... done IFS=$OLDIFS |
IFS is the “internal field separator” which defaults to a blank space. We save the original IFS and change it to a comma. Then once we’re done we restore the original IFS.
Here are some links to additional examples:
http://www.cyberciti.biz/faq/bash-for-loop/