Bash scripting Array concepts

The name of the pictureThe name of the pictureThe name of the pictureClash 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.............









share|improve this question









New contributor




naren viru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • 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














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.............









share|improve this question









New contributor




naren viru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • 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












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.............









share|improve this question









New contributor




naren viru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




naren viru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




naren viru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 3 hours ago





















New contributor




naren viru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









naren viru

213




213




New contributor




naren viru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





naren viru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






naren viru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • 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











  • 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










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






share|improve this answer


















  • 1




    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

















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






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    naren viru is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    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






























    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






    share|improve this answer


















    • 1




      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














    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






    share|improve this answer


















    • 1




      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












    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






    share|improve this answer














    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 22 mins ago

























    answered 3 hours ago









    Goro

    4,84052358




    4,84052358







    • 1




      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












    • 1




      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







    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












    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






    share|improve this answer
























      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






      share|improve this answer






















        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 20 mins ago









        glenn jackman

        48.1k365105




        48.1k365105




















            naren viru is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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.













             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            What does second last employer means? [closed]

            One-line joke