Bash scripting Array concepts
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I have two seperate lists. one contains 1000 hostnames and other contains 1000 associated IP's for that hosts. I need to generate config entries and push to my config file to add all hosts under our monitoring environment.
#!/bin/bash
Enter the hostnames
readarray hosts
Enter the IP's
readarray IP
how I iterate one by one in loop like below. I know how to iterate single array by using for i in "$hosts[@]"
, but in the case of two seperate lists, how to achieve iteration, anyone please assist me on this.
echo -e "nObject host ""$hosts"""
import = ""generic-service""
address = ""$IP""
group = ""Mom1-Ping""
"n" >> /etc/icinga2/zones.d/mom2/AP.conf
Example of first list (List 1):
sjc02-mfg-api01.example.com
sjc02-mfg-api02.example.com
sjc02-mfg-api03.example.com
upto 1000 hosts
Example of second list (List 2):
10.20.2.22
10.20.2.23
10.20.2.24
upto to 1000 IP's
Expected output:
Object host "sjc02-mfg-api01.example.com"
import = "generic-service"
address = "10.20.2.22"
group = "Mom01-Ping"
Object host "sjc02-mfg-api02.example.com"
import = "generic-service"
address = "10.20.2.23"
group = "Mom01-Ping"
Object host "sjc02-mfg-api03.example.com"
import = "generic-service"
address = "10.20.2.24"
group = "Mom01-Ping"
..........like this I need to generate to all 1000 hosts.............
shell-script icinga2
New contributor
 |Â
show 2 more comments
up vote
3
down vote
favorite
I have two seperate lists. one contains 1000 hostnames and other contains 1000 associated IP's for that hosts. I need to generate config entries and push to my config file to add all hosts under our monitoring environment.
#!/bin/bash
Enter the hostnames
readarray hosts
Enter the IP's
readarray IP
how I iterate one by one in loop like below. I know how to iterate single array by using for i in "$hosts[@]"
, but in the case of two seperate lists, how to achieve iteration, anyone please assist me on this.
echo -e "nObject host ""$hosts"""
import = ""generic-service""
address = ""$IP""
group = ""Mom1-Ping""
"n" >> /etc/icinga2/zones.d/mom2/AP.conf
Example of first list (List 1):
sjc02-mfg-api01.example.com
sjc02-mfg-api02.example.com
sjc02-mfg-api03.example.com
upto 1000 hosts
Example of second list (List 2):
10.20.2.22
10.20.2.23
10.20.2.24
upto to 1000 IP's
Expected output:
Object host "sjc02-mfg-api01.example.com"
import = "generic-service"
address = "10.20.2.22"
group = "Mom01-Ping"
Object host "sjc02-mfg-api02.example.com"
import = "generic-service"
address = "10.20.2.23"
group = "Mom01-Ping"
Object host "sjc02-mfg-api03.example.com"
import = "generic-service"
address = "10.20.2.24"
group = "Mom01-Ping"
..........like this I need to generate to all 1000 hosts.............
shell-script icinga2
New contributor
Hellp @naren viru. May I kindly ask you to include example of the two lists ? Thanks!
â Goro
3 hours ago
List 1: sjc02-mfg-api01.example.com sjc02-mfg-api02.example.com . List 2: 10.20.2.22 10.20.2.23 Expected Output: Object host "sjc02-mfg-api01.example.com" import = "generic-service" address = "10.20.2.22" group = "Mom01-Ping" Object host "sjc02-mfg-api02.example.com" import = "generic-service" address = "10.20.2.23" group = "Mom01-Ping" Like this I need to generate for 1000 devices
â naren viru
3 hours ago
need to read hostname from one list and read IP information from another list.
â naren viru
3 hours ago
And use both information to generate config entries
â naren viru
3 hours ago
1
Yes @goro, done it
â naren viru
3 hours ago
 |Â
show 2 more comments
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have two seperate lists. one contains 1000 hostnames and other contains 1000 associated IP's for that hosts. I need to generate config entries and push to my config file to add all hosts under our monitoring environment.
#!/bin/bash
Enter the hostnames
readarray hosts
Enter the IP's
readarray IP
how I iterate one by one in loop like below. I know how to iterate single array by using for i in "$hosts[@]"
, but in the case of two seperate lists, how to achieve iteration, anyone please assist me on this.
echo -e "nObject host ""$hosts"""
import = ""generic-service""
address = ""$IP""
group = ""Mom1-Ping""
"n" >> /etc/icinga2/zones.d/mom2/AP.conf
Example of first list (List 1):
sjc02-mfg-api01.example.com
sjc02-mfg-api02.example.com
sjc02-mfg-api03.example.com
upto 1000 hosts
Example of second list (List 2):
10.20.2.22
10.20.2.23
10.20.2.24
upto to 1000 IP's
Expected output:
Object host "sjc02-mfg-api01.example.com"
import = "generic-service"
address = "10.20.2.22"
group = "Mom01-Ping"
Object host "sjc02-mfg-api02.example.com"
import = "generic-service"
address = "10.20.2.23"
group = "Mom01-Ping"
Object host "sjc02-mfg-api03.example.com"
import = "generic-service"
address = "10.20.2.24"
group = "Mom01-Ping"
..........like this I need to generate to all 1000 hosts.............
shell-script icinga2
New contributor
I have two seperate lists. one contains 1000 hostnames and other contains 1000 associated IP's for that hosts. I need to generate config entries and push to my config file to add all hosts under our monitoring environment.
#!/bin/bash
Enter the hostnames
readarray hosts
Enter the IP's
readarray IP
how I iterate one by one in loop like below. I know how to iterate single array by using for i in "$hosts[@]"
, but in the case of two seperate lists, how to achieve iteration, anyone please assist me on this.
echo -e "nObject host ""$hosts"""
import = ""generic-service""
address = ""$IP""
group = ""Mom1-Ping""
"n" >> /etc/icinga2/zones.d/mom2/AP.conf
Example of first list (List 1):
sjc02-mfg-api01.example.com
sjc02-mfg-api02.example.com
sjc02-mfg-api03.example.com
upto 1000 hosts
Example of second list (List 2):
10.20.2.22
10.20.2.23
10.20.2.24
upto to 1000 IP's
Expected output:
Object host "sjc02-mfg-api01.example.com"
import = "generic-service"
address = "10.20.2.22"
group = "Mom01-Ping"
Object host "sjc02-mfg-api02.example.com"
import = "generic-service"
address = "10.20.2.23"
group = "Mom01-Ping"
Object host "sjc02-mfg-api03.example.com"
import = "generic-service"
address = "10.20.2.24"
group = "Mom01-Ping"
..........like this I need to generate to all 1000 hosts.............
shell-script icinga2
shell-script icinga2
New contributor
New contributor
edited 3 hours ago
New contributor
asked 3 hours ago
naren viru
213
213
New contributor
New contributor
Hellp @naren viru. May I kindly ask you to include example of the two lists ? Thanks!
â Goro
3 hours ago
List 1: sjc02-mfg-api01.example.com sjc02-mfg-api02.example.com . List 2: 10.20.2.22 10.20.2.23 Expected Output: Object host "sjc02-mfg-api01.example.com" import = "generic-service" address = "10.20.2.22" group = "Mom01-Ping" Object host "sjc02-mfg-api02.example.com" import = "generic-service" address = "10.20.2.23" group = "Mom01-Ping" Like this I need to generate for 1000 devices
â naren viru
3 hours ago
need to read hostname from one list and read IP information from another list.
â naren viru
3 hours ago
And use both information to generate config entries
â naren viru
3 hours ago
1
Yes @goro, done it
â naren viru
3 hours ago
 |Â
show 2 more comments
Hellp @naren viru. May I kindly ask you to include example of the two lists ? Thanks!
â Goro
3 hours ago
List 1: sjc02-mfg-api01.example.com sjc02-mfg-api02.example.com . List 2: 10.20.2.22 10.20.2.23 Expected Output: Object host "sjc02-mfg-api01.example.com" import = "generic-service" address = "10.20.2.22" group = "Mom01-Ping" Object host "sjc02-mfg-api02.example.com" import = "generic-service" address = "10.20.2.23" group = "Mom01-Ping" Like this I need to generate for 1000 devices
â naren viru
3 hours ago
need to read hostname from one list and read IP information from another list.
â naren viru
3 hours ago
And use both information to generate config entries
â naren viru
3 hours ago
1
Yes @goro, done it
â naren viru
3 hours ago
Hellp @naren viru. May I kindly ask you to include example of the two lists ? Thanks!
â Goro
3 hours ago
Hellp @naren viru. May I kindly ask you to include example of the two lists ? Thanks!
â Goro
3 hours ago
List 1: sjc02-mfg-api01.example.com sjc02-mfg-api02.example.com . List 2: 10.20.2.22 10.20.2.23 Expected Output: Object host "sjc02-mfg-api01.example.com" import = "generic-service" address = "10.20.2.22" group = "Mom01-Ping" Object host "sjc02-mfg-api02.example.com" import = "generic-service" address = "10.20.2.23" group = "Mom01-Ping" Like this I need to generate for 1000 devices
â naren viru
3 hours ago
List 1: sjc02-mfg-api01.example.com sjc02-mfg-api02.example.com . List 2: 10.20.2.22 10.20.2.23 Expected Output: Object host "sjc02-mfg-api01.example.com" import = "generic-service" address = "10.20.2.22" group = "Mom01-Ping" Object host "sjc02-mfg-api02.example.com" import = "generic-service" address = "10.20.2.23" group = "Mom01-Ping" Like this I need to generate for 1000 devices
â naren viru
3 hours ago
need to read hostname from one list and read IP information from another list.
â naren viru
3 hours ago
need to read hostname from one list and read IP information from another list.
â naren viru
3 hours ago
And use both information to generate config entries
â naren viru
3 hours ago
And use both information to generate config entries
â naren viru
3 hours ago
1
1
Yes @goro, done it
â naren viru
3 hours ago
Yes @goro, done it
â naren viru
3 hours ago
 |Â
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
5
down vote
Combine your both lists together using paste
. You can do it as follows:
#!/bin/bash
paste list1 list2 | while IFS=$'t' read -r L1 L2
do
echo "
Object host $L1
import = "generic-service"
address = $L2
group = "Mom01-Ping"
"
done
output:
Object host sjc02-mfg-api01.example.com
import = generic-service
address = 10.20.2.22
group = Mom01-Ping
Object host sjc02-mfg-api02.example.com
import = generic-service
address = 10.20.2.23
group = Mom01-Ping
Object host sjc02-mfg-api03.example.com
import = generic-service
address = 10.20.2.24
group = Mom01-Ping
1
You can useIFS=$'t'
in bash for a literal tab character.
â glenn jackman
24 mins ago
Dear @glenn jackman. Correct! Thank you so much for the note!. I updated the answer ;-)
â Goro
22 mins ago
add a comment |Â
up vote
2
down vote
You can iterate over the indices of the pair of arrays:
a1=(foo bar baz)
a2=(one two three)
for ((i=0; i < "$#a1[@]"; i++)); do
echo "$a1[i] => $a2[i]"
done
where $#a1[@]
is the size of the a1 array
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
Combine your both lists together using paste
. You can do it as follows:
#!/bin/bash
paste list1 list2 | while IFS=$'t' read -r L1 L2
do
echo "
Object host $L1
import = "generic-service"
address = $L2
group = "Mom01-Ping"
"
done
output:
Object host sjc02-mfg-api01.example.com
import = generic-service
address = 10.20.2.22
group = Mom01-Ping
Object host sjc02-mfg-api02.example.com
import = generic-service
address = 10.20.2.23
group = Mom01-Ping
Object host sjc02-mfg-api03.example.com
import = generic-service
address = 10.20.2.24
group = Mom01-Ping
1
You can useIFS=$'t'
in bash for a literal tab character.
â glenn jackman
24 mins ago
Dear @glenn jackman. Correct! Thank you so much for the note!. I updated the answer ;-)
â Goro
22 mins ago
add a comment |Â
up vote
5
down vote
Combine your both lists together using paste
. You can do it as follows:
#!/bin/bash
paste list1 list2 | while IFS=$'t' read -r L1 L2
do
echo "
Object host $L1
import = "generic-service"
address = $L2
group = "Mom01-Ping"
"
done
output:
Object host sjc02-mfg-api01.example.com
import = generic-service
address = 10.20.2.22
group = Mom01-Ping
Object host sjc02-mfg-api02.example.com
import = generic-service
address = 10.20.2.23
group = Mom01-Ping
Object host sjc02-mfg-api03.example.com
import = generic-service
address = 10.20.2.24
group = Mom01-Ping
1
You can useIFS=$'t'
in bash for a literal tab character.
â glenn jackman
24 mins ago
Dear @glenn jackman. Correct! Thank you so much for the note!. I updated the answer ;-)
â Goro
22 mins ago
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Combine your both lists together using paste
. You can do it as follows:
#!/bin/bash
paste list1 list2 | while IFS=$'t' read -r L1 L2
do
echo "
Object host $L1
import = "generic-service"
address = $L2
group = "Mom01-Ping"
"
done
output:
Object host sjc02-mfg-api01.example.com
import = generic-service
address = 10.20.2.22
group = Mom01-Ping
Object host sjc02-mfg-api02.example.com
import = generic-service
address = 10.20.2.23
group = Mom01-Ping
Object host sjc02-mfg-api03.example.com
import = generic-service
address = 10.20.2.24
group = Mom01-Ping
Combine your both lists together using paste
. You can do it as follows:
#!/bin/bash
paste list1 list2 | while IFS=$'t' read -r L1 L2
do
echo "
Object host $L1
import = "generic-service"
address = $L2
group = "Mom01-Ping"
"
done
output:
Object host sjc02-mfg-api01.example.com
import = generic-service
address = 10.20.2.22
group = Mom01-Ping
Object host sjc02-mfg-api02.example.com
import = generic-service
address = 10.20.2.23
group = Mom01-Ping
Object host sjc02-mfg-api03.example.com
import = generic-service
address = 10.20.2.24
group = Mom01-Ping
edited 22 mins ago
answered 3 hours ago
Goro
4,84052358
4,84052358
1
You can useIFS=$'t'
in bash for a literal tab character.
â glenn jackman
24 mins ago
Dear @glenn jackman. Correct! Thank you so much for the note!. I updated the answer ;-)
â Goro
22 mins ago
add a comment |Â
1
You can useIFS=$'t'
in bash for a literal tab character.
â glenn jackman
24 mins ago
Dear @glenn jackman. Correct! Thank you so much for the note!. I updated the answer ;-)
â Goro
22 mins ago
1
1
You can use
IFS=$'t'
in bash for a literal tab character.â glenn jackman
24 mins ago
You can use
IFS=$'t'
in bash for a literal tab character.â glenn jackman
24 mins ago
Dear @glenn jackman. Correct! Thank you so much for the note!. I updated the answer ;-)
â Goro
22 mins ago
Dear @glenn jackman. Correct! Thank you so much for the note!. I updated the answer ;-)
â Goro
22 mins ago
add a comment |Â
up vote
2
down vote
You can iterate over the indices of the pair of arrays:
a1=(foo bar baz)
a2=(one two three)
for ((i=0; i < "$#a1[@]"; i++)); do
echo "$a1[i] => $a2[i]"
done
where $#a1[@]
is the size of the a1 array
add a comment |Â
up vote
2
down vote
You can iterate over the indices of the pair of arrays:
a1=(foo bar baz)
a2=(one two three)
for ((i=0; i < "$#a1[@]"; i++)); do
echo "$a1[i] => $a2[i]"
done
where $#a1[@]
is the size of the a1 array
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can iterate over the indices of the pair of arrays:
a1=(foo bar baz)
a2=(one two three)
for ((i=0; i < "$#a1[@]"; i++)); do
echo "$a1[i] => $a2[i]"
done
where $#a1[@]
is the size of the a1 array
You can iterate over the indices of the pair of arrays:
a1=(foo bar baz)
a2=(one two three)
for ((i=0; i < "$#a1[@]"; i++)); do
echo "$a1[i] => $a2[i]"
done
where $#a1[@]
is the size of the a1 array
answered 20 mins ago
glenn jackman
48.1k365105
48.1k365105
add a comment |Â
add a comment |Â
naren viru is a new contributor. Be nice, and check out our Code of Conduct.
naren viru is a new contributor. Be nice, and check out our Code of Conduct.
naren viru is a new contributor. Be nice, and check out our Code of Conduct.
naren viru is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471802%2fbash-scripting-array-concepts%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Hellp @naren viru. May I kindly ask you to include example of the two lists ? Thanks!
â Goro
3 hours ago
List 1: sjc02-mfg-api01.example.com sjc02-mfg-api02.example.com . List 2: 10.20.2.22 10.20.2.23 Expected Output: Object host "sjc02-mfg-api01.example.com" import = "generic-service" address = "10.20.2.22" group = "Mom01-Ping" Object host "sjc02-mfg-api02.example.com" import = "generic-service" address = "10.20.2.23" group = "Mom01-Ping" Like this I need to generate for 1000 devices
â naren viru
3 hours ago
need to read hostname from one list and read IP information from another list.
â naren viru
3 hours ago
And use both information to generate config entries
â naren viru
3 hours ago
1
Yes @goro, done it
â naren viru
3 hours ago