# Loop with 2 variables in a bash script

I am attempting to make use of an API. I require to either do some sort of "for loop" that changes or makes use of 2 variables

In pseudo.

# Declare New Servers
newserver=(
box001
box002
box003
box004
box005
)

# Declare hostnames
newhostname=(
box001.domain.com
box002.domain.com
box003.domain.com
box004.domain.com
box005.domain.com
)


I require to change NEWSERVER and also NEWHOSTNAME in

http://server/api/duplicateobject.htm?id=2928&name=NEWSERVER&host=NEWHOSTNAME&targetid=3120


to make sure that it resembles this

http://server/api/duplicateobject.htm?id=2928&name=box001&host=box001.zcloud.com&targetid=3120


I simply require it to loop via every one of hosts detailed.

2
2022-06-07 14:40:44
Source Share

If both your selections coincide size, and also you simply intend to be able accessibility the very same index in each, below is an instance:

for ((i=0; i < ${#newserver[@]}; i++)); do echo${newserver[i]} ${newhostname[i]}; done;  4 2022-06-07 15:12:19 Source Your over selections and also: url="http://server/api/duplicateobject.htm?id=2928&name=NEWSERVER&host=NEWHOSTNAME&targetid=3120" for ((i=0; i <${#newserver[@]}; i++));
do
echo $url | sed 's/NEWSERVER/'${newserver[i]}'/;s/NEWHOSTNAME/'${newhostname[i]}'/' done  brings about: http://server/api/duplicateobject.htm?id=2928&name=box001&host=box001.domain.com&targetid=3120 http://server/api/duplicateobject.htm?id=2928&name=box002&host=box002.domain.com&targetid=3120 http://server/api/duplicateobject.htm?id=2928&name=box003&host=box003.domain.com&targetid=3120 http://server/api/duplicateobject.htm?id=2928&name=box004&host=box004.domain.com&targetid=3120 http://server/api/duplicateobject.htm?id=2928&name=box005&host=box005.domain.com&targetid=3120  2 2022-06-07 15:11:24 Source The standards isn't specified that well, yet this is just how I would certainly do it: hosts=( box001:box001.domain.com box002:box002.domain.com box003:box003.domain.com box004:box004.domain.com box005:box005.domain.com ) uri="http://server/api/duplicateobject.htm?id=2928&name=NEWSERVER&host=NEWHOSTNAME&targetid=3120" for host in "${hosts[@]}"; do
IFS=":" names=( $host ) hosturi="${uri/NEWSERVER/${names[0]}}" hosturi="${hosturi/NEWHOSTNAME/${names[1]}}" echo "$hosturi"
done


Outputs:

http://server/api/duplicateobject.htm?id=2928&name=box001&host=box001.domain.com&targetid=3120
http://server/api/duplicateobject.htm?id=2928&name=box002&host=box002.domain.com&targetid=3120
http://server/api/duplicateobject.htm?id=2928&name=box003&host=box003.domain.com&targetid=3120
http://server/api/duplicateobject.htm?id=2928&name=box004&host=box004.domain.com&targetid=3120
http://server/api/duplicateobject.htm?id=2928&name=box005&host=box005.domain.com&targetid=3120


• Keeps it clear which web server name refers which fqdn
• Its pure bash & makes use of no exterior anything

If the web server name is constantly mosting likely to be the first component of the fqdn, the array can be also less complex and also you can have bash number the web server name of the fqdn.

4
2022-06-07 15:11:20
Source

If you are making use of bash, you can make use of associative selections:

declare -A servers
servers=(
[box001]=box001.domain.com
[box002]=box002.domain.com
[box003]=box003.domain.com
[box004]=box004.domain.com
[box005]=box005.domain.com
)

for server in "${!servers[@]}" ; do hostname="${servers[$server]}" # do something with$server and \$hostname
done


The order that you iterate the web servers is not specified. For the above instance in my variation of bash on my host, the order is box001, box003, box002, box005 and also box004, YMMV

1
2022-06-07 15:10:19
Source