Looping a for with variables on a shell scriptShell script execution on multiple serversRunning any kind of login script when sh is linked to bashHow to use a password as an external variable in shell?Different configurations for ssh client depending on ip address or hostnameLinux user ssh connection disabled after execution of setfacl commandBash script with ssh not returning value of variablesChange default login shell to /bin/bash for ALL ldap users from LDAP server - not clientno user account can login via sshsign_and_send_pubkey: signing failed: agent refused operation CentOS 7 only between certain serversParallel ssh commands forking in background but keeping ssh open

Tiptoe or tiphoof? Adjusting words to better fit fantasy races

Trouble understanding the speech of overseas colleagues

What is the best translation for "slot" in the context of multiplayer video games?

Is there a problem with hiding "forgot password" until it's needed?

Why escape if the_content isnt?

A particular customize with green line and letters for subfloat

Short story about space worker geeks who zone out by 'listening' to radiation from stars

How easy is it to start Magic from scratch?

Return the Closest Prime Number

Gears on left are inverse to gears on right?

Is a stroke of luck acceptable after a series of unfavorable events?

You cannot touch me, but I can touch you, who am I?

Would a high gravity rocky planet be guaranteed to have an atmosphere?

Type int? vs type int

Lay out the Carpet

Is there a korbon needed for conversion?

Class Action - which options I have?

Method to test if a number is a perfect power?

Sequence of Tenses: Translating the subjunctive

How do we know the LHC results are robust?

Purchasing a ticket for someone else in another country?

How does the UK government determine the size of a mandate?

Sort a list by elements of another list

Different result between scanning in Epson's "color negative film" mode and scanning in positive -> invert curve in post?



Looping a for with variables on a shell script


Shell script execution on multiple serversRunning any kind of login script when sh is linked to bashHow to use a password as an external variable in shell?Different configurations for ssh client depending on ip address or hostnameLinux user ssh connection disabled after execution of setfacl commandBash script with ssh not returning value of variablesChange default login shell to /bin/bash for ALL ldap users from LDAP server - not clientno user account can login via sshsign_and_send_pubkey: signing failed: agent refused operation CentOS 7 only between certain serversParallel ssh commands forking in background but keeping ssh open













3















I have the following script that SSH to a server with a key and makes a lot of stuff there.



#!/usr/bin/env bash

ssh -i mykey.pem myuser@SERVER_IP << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH


(which I run it with sh scriptname.sh)



Now I want to to the same in another server, so I've to SSH to two different servers (ip_1 and ip_2) with two different .pem files (mykey1.pem and mykey2.pem).



So far I know how to loop the ips as follows:



#!/usr/bin/env bash

ip_list="ip_1 ip_2"

for ip in $ip_list; do
ssh -i mykey.pem myuser@$ip << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH
done


but now I would like to loop also to get the proper pem file. How can I archieve this? Maybe with another list? Can someone provide me an elegant solution?




  • ip_1 should use mykey1.pem


  • ip_2 should use mykey2.pem

Thanks in advance










share|improve this question









New contributor




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




















  • Don't use mykey1.pem but ip_1.pem. Then it's easy: ssh -i $ip.pem myuser@$ip << 'ENDSSH'

    – stoney
    yesterday















3















I have the following script that SSH to a server with a key and makes a lot of stuff there.



#!/usr/bin/env bash

ssh -i mykey.pem myuser@SERVER_IP << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH


(which I run it with sh scriptname.sh)



Now I want to to the same in another server, so I've to SSH to two different servers (ip_1 and ip_2) with two different .pem files (mykey1.pem and mykey2.pem).



So far I know how to loop the ips as follows:



#!/usr/bin/env bash

ip_list="ip_1 ip_2"

for ip in $ip_list; do
ssh -i mykey.pem myuser@$ip << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH
done


but now I would like to loop also to get the proper pem file. How can I archieve this? Maybe with another list? Can someone provide me an elegant solution?




  • ip_1 should use mykey1.pem


  • ip_2 should use mykey2.pem

Thanks in advance










share|improve this question









New contributor




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




















  • Don't use mykey1.pem but ip_1.pem. Then it's easy: ssh -i $ip.pem myuser@$ip << 'ENDSSH'

    – stoney
    yesterday













3












3








3








I have the following script that SSH to a server with a key and makes a lot of stuff there.



#!/usr/bin/env bash

ssh -i mykey.pem myuser@SERVER_IP << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH


(which I run it with sh scriptname.sh)



Now I want to to the same in another server, so I've to SSH to two different servers (ip_1 and ip_2) with two different .pem files (mykey1.pem and mykey2.pem).



So far I know how to loop the ips as follows:



#!/usr/bin/env bash

ip_list="ip_1 ip_2"

for ip in $ip_list; do
ssh -i mykey.pem myuser@$ip << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH
done


but now I would like to loop also to get the proper pem file. How can I archieve this? Maybe with another list? Can someone provide me an elegant solution?




  • ip_1 should use mykey1.pem


  • ip_2 should use mykey2.pem

Thanks in advance










share|improve this question









New contributor




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












I have the following script that SSH to a server with a key and makes a lot of stuff there.



#!/usr/bin/env bash

ssh -i mykey.pem myuser@SERVER_IP << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH


(which I run it with sh scriptname.sh)



Now I want to to the same in another server, so I've to SSH to two different servers (ip_1 and ip_2) with two different .pem files (mykey1.pem and mykey2.pem).



So far I know how to loop the ips as follows:



#!/usr/bin/env bash

ip_list="ip_1 ip_2"

for ip in $ip_list; do
ssh -i mykey.pem myuser@$ip << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH
done


but now I would like to loop also to get the proper pem file. How can I archieve this? Maybe with another list? Can someone provide me an elegant solution?




  • ip_1 should use mykey1.pem


  • ip_2 should use mykey2.pem

Thanks in advance







shell-script shell ssh






share|improve this question









New contributor




Avión 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




Avión 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 yesterday









GAD3R

27.5k1858114




27.5k1858114






New contributor




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









asked yesterday









AviónAvión

1192




1192




New contributor




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





New contributor





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






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












  • Don't use mykey1.pem but ip_1.pem. Then it's easy: ssh -i $ip.pem myuser@$ip << 'ENDSSH'

    – stoney
    yesterday

















  • Don't use mykey1.pem but ip_1.pem. Then it's easy: ssh -i $ip.pem myuser@$ip << 'ENDSSH'

    – stoney
    yesterday
















Don't use mykey1.pem but ip_1.pem. Then it's easy: ssh -i $ip.pem myuser@$ip << 'ENDSSH'

– stoney
yesterday





Don't use mykey1.pem but ip_1.pem. Then it's easy: ssh -i $ip.pem myuser@$ip << 'ENDSSH'

– stoney
yesterday










3 Answers
3






active

oldest

votes


















6














One way to do it is to use a while IFS=, read -r loop on a csv here-document.



#! /bin/sh -
while IFS=, read <&3 -r ip key; do
ssh -i "$key" "$ip" << ENDSSH
...
ENDSSH
done 3<< ENDCSV
10.0.0.1,p1.pem
10.0.0.2,p2.pem
ENDCSV


Then you don't even need to require users to install bash. If portability is not a concern, you could use zsh instead which supports looping over more than one variable.



#! /usr/bin/env zsh
for ip key (
10.0.0.1 p1.pem
10.0.0.2 p2.pem
) ssh -i $key $ip << ENDSSH
...
ENDSSH





share|improve this answer
































    6














    Since you're using bash, you can use associative arrays:



    #!/usr/bin/env bash

    declare -A ip_list=(["ip_1"]="mykey1.pem" ["ip_2"]="mykey2.pem")

    for ip in "$!ip_list[@]"; do
    ssh -i "$ip_list[$ip]" myuser@"$ip" << 'ENDSSH'
    [A LOT OF STUFF]
    ENDSSH
    done


    Note that associative arrays, unlike regular indexed arrays, are not saved in a specific order, so there is no guarantee that ip_1 will be processed before ip_2.




    If you need to use a simple, POSIX compatible shell, create a file with the ip and key files, one per line:



    $ cat iplist.txt
    ip1 mykey1.pem
    ip2 mykey2.pem


    Then, use this script:



    #!/bin/sh

    while read -r ip key; do
    ssh -i "$key" myuser@"$ip" << 'ENDSSH'
    [A LOT OF STUFF]
    ENDSSH
    done


    And run it with:



    sh /path/to/script < /path/to/iplist.txt


    But if you go that route, Stéphane's approach is better.






    share|improve this answer

























    • I'm running it with sh (I cant change that), so it seems I cannot use arrays.

      – Avión
      yesterday







    • 3





      @Avión what do you mean? You have #!/usr/bin/env bash in your script. Just make the script executable (chmod a+x /path/to/script) and then run it directly: /path/to/script. Alternatively, run it with bash /path/to/script. The only reason you're using sh is because you're calling it with sh /path/to/script.

      – terdon
      yesterday











    • @StéphaneChazelas good point. Done, thanks.

      – terdon
      yesterday


















    1














    Your original script can be made slightly more portable via set built-in, plus have ip and key go together as one string, colon separated. We later can use prefix and suffix removal to extract corresponding entry



    #!/usr/bin/env bash

    # Set positional parameters
    # Example ip addresses
    set -- 192.168.0.1:mykey1 192.168.1.1:mykey2

    # iterating without specifying 'in' assumes positiona parameters
    for host; do
    ssh -i $host##*:.pem myuser@$host%%:* << 'ENDSSH'
    [A LOT OF STUFF]
    ENDSSH

    done





    share|improve this answer


















    • 2





      set is not really helping here. You could just as well do for host in 192.168.0.1:mykey1 192.168.1.1:mykey2; do. Note that : is not the best choice of delimiter as it's found in IPv6 addresses. You also need to quote parameter expansions to prevent split+glob

      – Stéphane Chazelas
      yesterday











    • All fair points. Semicolon could serve a better delimiter, maybe ?

      – Sergiy Kolodyazhnyy
      yesterday










    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Avión 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%2f508706%2flooping-a-for-with-variables-on-a-shell-script%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    One way to do it is to use a while IFS=, read -r loop on a csv here-document.



    #! /bin/sh -
    while IFS=, read <&3 -r ip key; do
    ssh -i "$key" "$ip" << ENDSSH
    ...
    ENDSSH
    done 3<< ENDCSV
    10.0.0.1,p1.pem
    10.0.0.2,p2.pem
    ENDCSV


    Then you don't even need to require users to install bash. If portability is not a concern, you could use zsh instead which supports looping over more than one variable.



    #! /usr/bin/env zsh
    for ip key (
    10.0.0.1 p1.pem
    10.0.0.2 p2.pem
    ) ssh -i $key $ip << ENDSSH
    ...
    ENDSSH





    share|improve this answer





























      6














      One way to do it is to use a while IFS=, read -r loop on a csv here-document.



      #! /bin/sh -
      while IFS=, read <&3 -r ip key; do
      ssh -i "$key" "$ip" << ENDSSH
      ...
      ENDSSH
      done 3<< ENDCSV
      10.0.0.1,p1.pem
      10.0.0.2,p2.pem
      ENDCSV


      Then you don't even need to require users to install bash. If portability is not a concern, you could use zsh instead which supports looping over more than one variable.



      #! /usr/bin/env zsh
      for ip key (
      10.0.0.1 p1.pem
      10.0.0.2 p2.pem
      ) ssh -i $key $ip << ENDSSH
      ...
      ENDSSH





      share|improve this answer



























        6












        6








        6







        One way to do it is to use a while IFS=, read -r loop on a csv here-document.



        #! /bin/sh -
        while IFS=, read <&3 -r ip key; do
        ssh -i "$key" "$ip" << ENDSSH
        ...
        ENDSSH
        done 3<< ENDCSV
        10.0.0.1,p1.pem
        10.0.0.2,p2.pem
        ENDCSV


        Then you don't even need to require users to install bash. If portability is not a concern, you could use zsh instead which supports looping over more than one variable.



        #! /usr/bin/env zsh
        for ip key (
        10.0.0.1 p1.pem
        10.0.0.2 p2.pem
        ) ssh -i $key $ip << ENDSSH
        ...
        ENDSSH





        share|improve this answer















        One way to do it is to use a while IFS=, read -r loop on a csv here-document.



        #! /bin/sh -
        while IFS=, read <&3 -r ip key; do
        ssh -i "$key" "$ip" << ENDSSH
        ...
        ENDSSH
        done 3<< ENDCSV
        10.0.0.1,p1.pem
        10.0.0.2,p2.pem
        ENDCSV


        Then you don't even need to require users to install bash. If portability is not a concern, you could use zsh instead which supports looping over more than one variable.



        #! /usr/bin/env zsh
        for ip key (
        10.0.0.1 p1.pem
        10.0.0.2 p2.pem
        ) ssh -i $key $ip << ENDSSH
        ...
        ENDSSH






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        Stéphane ChazelasStéphane Chazelas

        311k57589946




        311k57589946























            6














            Since you're using bash, you can use associative arrays:



            #!/usr/bin/env bash

            declare -A ip_list=(["ip_1"]="mykey1.pem" ["ip_2"]="mykey2.pem")

            for ip in "$!ip_list[@]"; do
            ssh -i "$ip_list[$ip]" myuser@"$ip" << 'ENDSSH'
            [A LOT OF STUFF]
            ENDSSH
            done


            Note that associative arrays, unlike regular indexed arrays, are not saved in a specific order, so there is no guarantee that ip_1 will be processed before ip_2.




            If you need to use a simple, POSIX compatible shell, create a file with the ip and key files, one per line:



            $ cat iplist.txt
            ip1 mykey1.pem
            ip2 mykey2.pem


            Then, use this script:



            #!/bin/sh

            while read -r ip key; do
            ssh -i "$key" myuser@"$ip" << 'ENDSSH'
            [A LOT OF STUFF]
            ENDSSH
            done


            And run it with:



            sh /path/to/script < /path/to/iplist.txt


            But if you go that route, Stéphane's approach is better.






            share|improve this answer

























            • I'm running it with sh (I cant change that), so it seems I cannot use arrays.

              – Avión
              yesterday







            • 3





              @Avión what do you mean? You have #!/usr/bin/env bash in your script. Just make the script executable (chmod a+x /path/to/script) and then run it directly: /path/to/script. Alternatively, run it with bash /path/to/script. The only reason you're using sh is because you're calling it with sh /path/to/script.

              – terdon
              yesterday











            • @StéphaneChazelas good point. Done, thanks.

              – terdon
              yesterday















            6














            Since you're using bash, you can use associative arrays:



            #!/usr/bin/env bash

            declare -A ip_list=(["ip_1"]="mykey1.pem" ["ip_2"]="mykey2.pem")

            for ip in "$!ip_list[@]"; do
            ssh -i "$ip_list[$ip]" myuser@"$ip" << 'ENDSSH'
            [A LOT OF STUFF]
            ENDSSH
            done


            Note that associative arrays, unlike regular indexed arrays, are not saved in a specific order, so there is no guarantee that ip_1 will be processed before ip_2.




            If you need to use a simple, POSIX compatible shell, create a file with the ip and key files, one per line:



            $ cat iplist.txt
            ip1 mykey1.pem
            ip2 mykey2.pem


            Then, use this script:



            #!/bin/sh

            while read -r ip key; do
            ssh -i "$key" myuser@"$ip" << 'ENDSSH'
            [A LOT OF STUFF]
            ENDSSH
            done


            And run it with:



            sh /path/to/script < /path/to/iplist.txt


            But if you go that route, Stéphane's approach is better.






            share|improve this answer

























            • I'm running it with sh (I cant change that), so it seems I cannot use arrays.

              – Avión
              yesterday







            • 3





              @Avión what do you mean? You have #!/usr/bin/env bash in your script. Just make the script executable (chmod a+x /path/to/script) and then run it directly: /path/to/script. Alternatively, run it with bash /path/to/script. The only reason you're using sh is because you're calling it with sh /path/to/script.

              – terdon
              yesterday











            • @StéphaneChazelas good point. Done, thanks.

              – terdon
              yesterday













            6












            6








            6







            Since you're using bash, you can use associative arrays:



            #!/usr/bin/env bash

            declare -A ip_list=(["ip_1"]="mykey1.pem" ["ip_2"]="mykey2.pem")

            for ip in "$!ip_list[@]"; do
            ssh -i "$ip_list[$ip]" myuser@"$ip" << 'ENDSSH'
            [A LOT OF STUFF]
            ENDSSH
            done


            Note that associative arrays, unlike regular indexed arrays, are not saved in a specific order, so there is no guarantee that ip_1 will be processed before ip_2.




            If you need to use a simple, POSIX compatible shell, create a file with the ip and key files, one per line:



            $ cat iplist.txt
            ip1 mykey1.pem
            ip2 mykey2.pem


            Then, use this script:



            #!/bin/sh

            while read -r ip key; do
            ssh -i "$key" myuser@"$ip" << 'ENDSSH'
            [A LOT OF STUFF]
            ENDSSH
            done


            And run it with:



            sh /path/to/script < /path/to/iplist.txt


            But if you go that route, Stéphane's approach is better.






            share|improve this answer















            Since you're using bash, you can use associative arrays:



            #!/usr/bin/env bash

            declare -A ip_list=(["ip_1"]="mykey1.pem" ["ip_2"]="mykey2.pem")

            for ip in "$!ip_list[@]"; do
            ssh -i "$ip_list[$ip]" myuser@"$ip" << 'ENDSSH'
            [A LOT OF STUFF]
            ENDSSH
            done


            Note that associative arrays, unlike regular indexed arrays, are not saved in a specific order, so there is no guarantee that ip_1 will be processed before ip_2.




            If you need to use a simple, POSIX compatible shell, create a file with the ip and key files, one per line:



            $ cat iplist.txt
            ip1 mykey1.pem
            ip2 mykey2.pem


            Then, use this script:



            #!/bin/sh

            while read -r ip key; do
            ssh -i "$key" myuser@"$ip" << 'ENDSSH'
            [A LOT OF STUFF]
            ENDSSH
            done


            And run it with:



            sh /path/to/script < /path/to/iplist.txt


            But if you go that route, Stéphane's approach is better.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited yesterday

























            answered yesterday









            terdonterdon

            133k32265444




            133k32265444












            • I'm running it with sh (I cant change that), so it seems I cannot use arrays.

              – Avión
              yesterday







            • 3





              @Avión what do you mean? You have #!/usr/bin/env bash in your script. Just make the script executable (chmod a+x /path/to/script) and then run it directly: /path/to/script. Alternatively, run it with bash /path/to/script. The only reason you're using sh is because you're calling it with sh /path/to/script.

              – terdon
              yesterday











            • @StéphaneChazelas good point. Done, thanks.

              – terdon
              yesterday

















            • I'm running it with sh (I cant change that), so it seems I cannot use arrays.

              – Avión
              yesterday







            • 3





              @Avión what do you mean? You have #!/usr/bin/env bash in your script. Just make the script executable (chmod a+x /path/to/script) and then run it directly: /path/to/script. Alternatively, run it with bash /path/to/script. The only reason you're using sh is because you're calling it with sh /path/to/script.

              – terdon
              yesterday











            • @StéphaneChazelas good point. Done, thanks.

              – terdon
              yesterday
















            I'm running it with sh (I cant change that), so it seems I cannot use arrays.

            – Avión
            yesterday






            I'm running it with sh (I cant change that), so it seems I cannot use arrays.

            – Avión
            yesterday





            3




            3





            @Avión what do you mean? You have #!/usr/bin/env bash in your script. Just make the script executable (chmod a+x /path/to/script) and then run it directly: /path/to/script. Alternatively, run it with bash /path/to/script. The only reason you're using sh is because you're calling it with sh /path/to/script.

            – terdon
            yesterday





            @Avión what do you mean? You have #!/usr/bin/env bash in your script. Just make the script executable (chmod a+x /path/to/script) and then run it directly: /path/to/script. Alternatively, run it with bash /path/to/script. The only reason you're using sh is because you're calling it with sh /path/to/script.

            – terdon
            yesterday













            @StéphaneChazelas good point. Done, thanks.

            – terdon
            yesterday





            @StéphaneChazelas good point. Done, thanks.

            – terdon
            yesterday











            1














            Your original script can be made slightly more portable via set built-in, plus have ip and key go together as one string, colon separated. We later can use prefix and suffix removal to extract corresponding entry



            #!/usr/bin/env bash

            # Set positional parameters
            # Example ip addresses
            set -- 192.168.0.1:mykey1 192.168.1.1:mykey2

            # iterating without specifying 'in' assumes positiona parameters
            for host; do
            ssh -i $host##*:.pem myuser@$host%%:* << 'ENDSSH'
            [A LOT OF STUFF]
            ENDSSH

            done





            share|improve this answer


















            • 2





              set is not really helping here. You could just as well do for host in 192.168.0.1:mykey1 192.168.1.1:mykey2; do. Note that : is not the best choice of delimiter as it's found in IPv6 addresses. You also need to quote parameter expansions to prevent split+glob

              – Stéphane Chazelas
              yesterday











            • All fair points. Semicolon could serve a better delimiter, maybe ?

              – Sergiy Kolodyazhnyy
              yesterday















            1














            Your original script can be made slightly more portable via set built-in, plus have ip and key go together as one string, colon separated. We later can use prefix and suffix removal to extract corresponding entry



            #!/usr/bin/env bash

            # Set positional parameters
            # Example ip addresses
            set -- 192.168.0.1:mykey1 192.168.1.1:mykey2

            # iterating without specifying 'in' assumes positiona parameters
            for host; do
            ssh -i $host##*:.pem myuser@$host%%:* << 'ENDSSH'
            [A LOT OF STUFF]
            ENDSSH

            done





            share|improve this answer


















            • 2





              set is not really helping here. You could just as well do for host in 192.168.0.1:mykey1 192.168.1.1:mykey2; do. Note that : is not the best choice of delimiter as it's found in IPv6 addresses. You also need to quote parameter expansions to prevent split+glob

              – Stéphane Chazelas
              yesterday











            • All fair points. Semicolon could serve a better delimiter, maybe ?

              – Sergiy Kolodyazhnyy
              yesterday













            1












            1








            1







            Your original script can be made slightly more portable via set built-in, plus have ip and key go together as one string, colon separated. We later can use prefix and suffix removal to extract corresponding entry



            #!/usr/bin/env bash

            # Set positional parameters
            # Example ip addresses
            set -- 192.168.0.1:mykey1 192.168.1.1:mykey2

            # iterating without specifying 'in' assumes positiona parameters
            for host; do
            ssh -i $host##*:.pem myuser@$host%%:* << 'ENDSSH'
            [A LOT OF STUFF]
            ENDSSH

            done





            share|improve this answer













            Your original script can be made slightly more portable via set built-in, plus have ip and key go together as one string, colon separated. We later can use prefix and suffix removal to extract corresponding entry



            #!/usr/bin/env bash

            # Set positional parameters
            # Example ip addresses
            set -- 192.168.0.1:mykey1 192.168.1.1:mykey2

            # iterating without specifying 'in' assumes positiona parameters
            for host; do
            ssh -i $host##*:.pem myuser@$host%%:* << 'ENDSSH'
            [A LOT OF STUFF]
            ENDSSH

            done






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            Sergiy KolodyazhnyySergiy Kolodyazhnyy

            10.7k42763




            10.7k42763







            • 2





              set is not really helping here. You could just as well do for host in 192.168.0.1:mykey1 192.168.1.1:mykey2; do. Note that : is not the best choice of delimiter as it's found in IPv6 addresses. You also need to quote parameter expansions to prevent split+glob

              – Stéphane Chazelas
              yesterday











            • All fair points. Semicolon could serve a better delimiter, maybe ?

              – Sergiy Kolodyazhnyy
              yesterday












            • 2





              set is not really helping here. You could just as well do for host in 192.168.0.1:mykey1 192.168.1.1:mykey2; do. Note that : is not the best choice of delimiter as it's found in IPv6 addresses. You also need to quote parameter expansions to prevent split+glob

              – Stéphane Chazelas
              yesterday











            • All fair points. Semicolon could serve a better delimiter, maybe ?

              – Sergiy Kolodyazhnyy
              yesterday







            2




            2





            set is not really helping here. You could just as well do for host in 192.168.0.1:mykey1 192.168.1.1:mykey2; do. Note that : is not the best choice of delimiter as it's found in IPv6 addresses. You also need to quote parameter expansions to prevent split+glob

            – Stéphane Chazelas
            yesterday





            set is not really helping here. You could just as well do for host in 192.168.0.1:mykey1 192.168.1.1:mykey2; do. Note that : is not the best choice of delimiter as it's found in IPv6 addresses. You also need to quote parameter expansions to prevent split+glob

            – Stéphane Chazelas
            yesterday













            All fair points. Semicolon could serve a better delimiter, maybe ?

            – Sergiy Kolodyazhnyy
            yesterday





            All fair points. Semicolon could serve a better delimiter, maybe ?

            – Sergiy Kolodyazhnyy
            yesterday










            Avión is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            Avión is a new contributor. Be nice, and check out our Code of Conduct.












            Avión is a new contributor. Be nice, and check out our Code of Conduct.











            Avión is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f508706%2flooping-a-for-with-variables-on-a-shell-script%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

            Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

            Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.