Bash array script, combine

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















I'm trying to write bash script, which will automatically read all file names in current and custom directories, then apply new created docker image' names to finded yml files via kubectl, then read from two arrays image names and full registry names:



declare -a IMG_ARRAY=`docker images | awk '/dev2/ && /latest/' | awk 'print $1' | sed ':a;N;$!ba;s/n/ /g'`
declare -a IMG_NAME=`docker images | awk '/dev2/ && /latest/' | awk 'print $1' | awk -F'/' 'print $3' | cut -f1 -d"." | sed ':a;N;$!ba;s/n/ /g'`

IFS=' ' read -r -a array <<< "$IMG_NAME"
for element in "$array[@]"
do
kubectl set image deployment/$IMG_NAME $IMG_NAME=$IMG_ARRAY --record
kubectl rollout status deployment/$IMG_NAME
done


Both arrays has same number of indexes. My loop should take first indexes from IMG_NAME and put into kubectl commands for every array index. For now it is taking whole array....










share|improve this question






















  • It would be easier to help if you would show the output of docker images as a separate code block.

    – Bodo
    Jan 17 at 12:33











  • IMG_NAME: asset_dev2 omnibus_dev2 saferwatch_dev2 stripe_dev2 organization_dev2 register_dev2

    – user37033
    Jan 17 at 12:48











  • IMG_ARRAY: registry/lsengine_dev2 registry/loadselect_dev2 registry/invoice_dev2 ...

    – user37033
    Jan 17 at 12:49











  • I would prefer to see a separate code block that shows example output of docker images without your post-processing. I want to see the output lines, not what you have combined into variables IMG_NAME and IMG_ARRAY

    – Bodo
    Jan 17 at 12:59















0















I'm trying to write bash script, which will automatically read all file names in current and custom directories, then apply new created docker image' names to finded yml files via kubectl, then read from two arrays image names and full registry names:



declare -a IMG_ARRAY=`docker images | awk '/dev2/ && /latest/' | awk 'print $1' | sed ':a;N;$!ba;s/n/ /g'`
declare -a IMG_NAME=`docker images | awk '/dev2/ && /latest/' | awk 'print $1' | awk -F'/' 'print $3' | cut -f1 -d"." | sed ':a;N;$!ba;s/n/ /g'`

IFS=' ' read -r -a array <<< "$IMG_NAME"
for element in "$array[@]"
do
kubectl set image deployment/$IMG_NAME $IMG_NAME=$IMG_ARRAY --record
kubectl rollout status deployment/$IMG_NAME
done


Both arrays has same number of indexes. My loop should take first indexes from IMG_NAME and put into kubectl commands for every array index. For now it is taking whole array....










share|improve this question






















  • It would be easier to help if you would show the output of docker images as a separate code block.

    – Bodo
    Jan 17 at 12:33











  • IMG_NAME: asset_dev2 omnibus_dev2 saferwatch_dev2 stripe_dev2 organization_dev2 register_dev2

    – user37033
    Jan 17 at 12:48











  • IMG_ARRAY: registry/lsengine_dev2 registry/loadselect_dev2 registry/invoice_dev2 ...

    – user37033
    Jan 17 at 12:49











  • I would prefer to see a separate code block that shows example output of docker images without your post-processing. I want to see the output lines, not what you have combined into variables IMG_NAME and IMG_ARRAY

    – Bodo
    Jan 17 at 12:59













0












0








0








I'm trying to write bash script, which will automatically read all file names in current and custom directories, then apply new created docker image' names to finded yml files via kubectl, then read from two arrays image names and full registry names:



declare -a IMG_ARRAY=`docker images | awk '/dev2/ && /latest/' | awk 'print $1' | sed ':a;N;$!ba;s/n/ /g'`
declare -a IMG_NAME=`docker images | awk '/dev2/ && /latest/' | awk 'print $1' | awk -F'/' 'print $3' | cut -f1 -d"." | sed ':a;N;$!ba;s/n/ /g'`

IFS=' ' read -r -a array <<< "$IMG_NAME"
for element in "$array[@]"
do
kubectl set image deployment/$IMG_NAME $IMG_NAME=$IMG_ARRAY --record
kubectl rollout status deployment/$IMG_NAME
done


Both arrays has same number of indexes. My loop should take first indexes from IMG_NAME and put into kubectl commands for every array index. For now it is taking whole array....










share|improve this question














I'm trying to write bash script, which will automatically read all file names in current and custom directories, then apply new created docker image' names to finded yml files via kubectl, then read from two arrays image names and full registry names:



declare -a IMG_ARRAY=`docker images | awk '/dev2/ && /latest/' | awk 'print $1' | sed ':a;N;$!ba;s/n/ /g'`
declare -a IMG_NAME=`docker images | awk '/dev2/ && /latest/' | awk 'print $1' | awk -F'/' 'print $3' | cut -f1 -d"." | sed ':a;N;$!ba;s/n/ /g'`

IFS=' ' read -r -a array <<< "$IMG_NAME"
for element in "$array[@]"
do
kubectl set image deployment/$IMG_NAME $IMG_NAME=$IMG_ARRAY --record
kubectl rollout status deployment/$IMG_NAME
done


Both arrays has same number of indexes. My loop should take first indexes from IMG_NAME and put into kubectl commands for every array index. For now it is taking whole array....







bash array






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 17 at 11:37









user37033user37033

191




191












  • It would be easier to help if you would show the output of docker images as a separate code block.

    – Bodo
    Jan 17 at 12:33











  • IMG_NAME: asset_dev2 omnibus_dev2 saferwatch_dev2 stripe_dev2 organization_dev2 register_dev2

    – user37033
    Jan 17 at 12:48











  • IMG_ARRAY: registry/lsengine_dev2 registry/loadselect_dev2 registry/invoice_dev2 ...

    – user37033
    Jan 17 at 12:49











  • I would prefer to see a separate code block that shows example output of docker images without your post-processing. I want to see the output lines, not what you have combined into variables IMG_NAME and IMG_ARRAY

    – Bodo
    Jan 17 at 12:59

















  • It would be easier to help if you would show the output of docker images as a separate code block.

    – Bodo
    Jan 17 at 12:33











  • IMG_NAME: asset_dev2 omnibus_dev2 saferwatch_dev2 stripe_dev2 organization_dev2 register_dev2

    – user37033
    Jan 17 at 12:48











  • IMG_ARRAY: registry/lsengine_dev2 registry/loadselect_dev2 registry/invoice_dev2 ...

    – user37033
    Jan 17 at 12:49











  • I would prefer to see a separate code block that shows example output of docker images without your post-processing. I want to see the output lines, not what you have combined into variables IMG_NAME and IMG_ARRAY

    – Bodo
    Jan 17 at 12:59
















It would be easier to help if you would show the output of docker images as a separate code block.

– Bodo
Jan 17 at 12:33





It would be easier to help if you would show the output of docker images as a separate code block.

– Bodo
Jan 17 at 12:33













IMG_NAME: asset_dev2 omnibus_dev2 saferwatch_dev2 stripe_dev2 organization_dev2 register_dev2

– user37033
Jan 17 at 12:48





IMG_NAME: asset_dev2 omnibus_dev2 saferwatch_dev2 stripe_dev2 organization_dev2 register_dev2

– user37033
Jan 17 at 12:48













IMG_ARRAY: registry/lsengine_dev2 registry/loadselect_dev2 registry/invoice_dev2 ...

– user37033
Jan 17 at 12:49





IMG_ARRAY: registry/lsengine_dev2 registry/loadselect_dev2 registry/invoice_dev2 ...

– user37033
Jan 17 at 12:49













I would prefer to see a separate code block that shows example output of docker images without your post-processing. I want to see the output lines, not what you have combined into variables IMG_NAME and IMG_ARRAY

– Bodo
Jan 17 at 12:59





I would prefer to see a separate code block that shows example output of docker images without your post-processing. I want to see the output lines, not what you have combined into variables IMG_NAME and IMG_ARRAY

– Bodo
Jan 17 at 12:59










2 Answers
2






active

oldest

votes


















1














declare -a IMG_ARRAY=`...`


This doesn't create much of an array, all the output from the command substitution is assigned to element zero of the array. The actual array assignment syntax is name=(elem1 elem2 ... ), i.e. with parenthesis and the elements as distinct words.



You could use word splitting to separate the output to elements, but that still requires the parens, and you're subject to IFS and globbing. declare -a aaa=( $(echo foo bar) ) creates the two elements foo and bar. Note that it splits on the space between the words, not just on newlines.



Using mapfile/readarray is probably better here, since it's explicitly made for reading lines to an array. The command line help text (help mapfile) describes this:



mapfile: mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from the standard input into an indexed array variable.

Read lines from the standard input into the indexed array variable ARRAY, or
from file descriptor FD if the -u option is supplied. The variable MAPFILE
is the default ARRAY.





share|improve this answer






























    1














    My understanding is that you want to have the processed output of docker images in two arrays where every array element corresponds to a line of the processed output.



    This script is untested because I neither know the output of docker images nor do I know the command syntax for kubectl.



    mapfile -t IMG_ARRAY < <(docker images | awk '/dev2/ && /latest/' | awk 'print $1' | sed ':a;N;$!ba;s/n/ /g')
    mapfile -t IMG_NAME < <(docker images | awk '/dev2/ && /latest/' | awk 'print $1' | awk -F'/' 'print $3' | cut -f1 -d"." | sed ':a;N;$!ba;s/n/ /g')

    total=$#IMG_NAME[*]

    for (( i=0; i<$(( $total )); i++ ))
    do
    kubectl set image deployment/$IMG_NAME[$i] $IMG_NAME[$i]=$IMG_ARRAY[$i] --record
    kubectl rollout status deployment/$IMG_NAME[i]
    done


    See https://www.cyberciti.biz/faq/bash-iterate-array/ and https://mywiki.wooledge.org/BashFAQ/005 for explanations.



    Instead of



    total=$#IMG_NAME[*]

    for (( i=0; i<$(( $total )); i++ ))


    you can also use



    for i in $!IMG_NAME[@]


    see https://stackoverflow.com/questions/6723426/looping-over-arrays-printing-both-index-and-value






    share|improve this answer

























    • You could also use array indirection to loop over the indices directly for i in $!arr[@] I think

      – steeldriver
      Jan 17 at 13:02










    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
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495032%2fbash-array-script-combine%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    declare -a IMG_ARRAY=`...`


    This doesn't create much of an array, all the output from the command substitution is assigned to element zero of the array. The actual array assignment syntax is name=(elem1 elem2 ... ), i.e. with parenthesis and the elements as distinct words.



    You could use word splitting to separate the output to elements, but that still requires the parens, and you're subject to IFS and globbing. declare -a aaa=( $(echo foo bar) ) creates the two elements foo and bar. Note that it splits on the space between the words, not just on newlines.



    Using mapfile/readarray is probably better here, since it's explicitly made for reading lines to an array. The command line help text (help mapfile) describes this:



    mapfile: mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
    Read lines from the standard input into an indexed array variable.

    Read lines from the standard input into the indexed array variable ARRAY, or
    from file descriptor FD if the -u option is supplied. The variable MAPFILE
    is the default ARRAY.





    share|improve this answer



























      1














      declare -a IMG_ARRAY=`...`


      This doesn't create much of an array, all the output from the command substitution is assigned to element zero of the array. The actual array assignment syntax is name=(elem1 elem2 ... ), i.e. with parenthesis and the elements as distinct words.



      You could use word splitting to separate the output to elements, but that still requires the parens, and you're subject to IFS and globbing. declare -a aaa=( $(echo foo bar) ) creates the two elements foo and bar. Note that it splits on the space between the words, not just on newlines.



      Using mapfile/readarray is probably better here, since it's explicitly made for reading lines to an array. The command line help text (help mapfile) describes this:



      mapfile: mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
      Read lines from the standard input into an indexed array variable.

      Read lines from the standard input into the indexed array variable ARRAY, or
      from file descriptor FD if the -u option is supplied. The variable MAPFILE
      is the default ARRAY.





      share|improve this answer

























        1












        1








        1







        declare -a IMG_ARRAY=`...`


        This doesn't create much of an array, all the output from the command substitution is assigned to element zero of the array. The actual array assignment syntax is name=(elem1 elem2 ... ), i.e. with parenthesis and the elements as distinct words.



        You could use word splitting to separate the output to elements, but that still requires the parens, and you're subject to IFS and globbing. declare -a aaa=( $(echo foo bar) ) creates the two elements foo and bar. Note that it splits on the space between the words, not just on newlines.



        Using mapfile/readarray is probably better here, since it's explicitly made for reading lines to an array. The command line help text (help mapfile) describes this:



        mapfile: mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
        Read lines from the standard input into an indexed array variable.

        Read lines from the standard input into the indexed array variable ARRAY, or
        from file descriptor FD if the -u option is supplied. The variable MAPFILE
        is the default ARRAY.





        share|improve this answer













        declare -a IMG_ARRAY=`...`


        This doesn't create much of an array, all the output from the command substitution is assigned to element zero of the array. The actual array assignment syntax is name=(elem1 elem2 ... ), i.e. with parenthesis and the elements as distinct words.



        You could use word splitting to separate the output to elements, but that still requires the parens, and you're subject to IFS and globbing. declare -a aaa=( $(echo foo bar) ) creates the two elements foo and bar. Note that it splits on the space between the words, not just on newlines.



        Using mapfile/readarray is probably better here, since it's explicitly made for reading lines to an array. The command line help text (help mapfile) describes this:



        mapfile: mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
        Read lines from the standard input into an indexed array variable.

        Read lines from the standard input into the indexed array variable ARRAY, or
        from file descriptor FD if the -u option is supplied. The variable MAPFILE
        is the default ARRAY.






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 17 at 13:17









        ilkkachuilkkachu

        57.7k888163




        57.7k888163























            1














            My understanding is that you want to have the processed output of docker images in two arrays where every array element corresponds to a line of the processed output.



            This script is untested because I neither know the output of docker images nor do I know the command syntax for kubectl.



            mapfile -t IMG_ARRAY < <(docker images | awk '/dev2/ && /latest/' | awk 'print $1' | sed ':a;N;$!ba;s/n/ /g')
            mapfile -t IMG_NAME < <(docker images | awk '/dev2/ && /latest/' | awk 'print $1' | awk -F'/' 'print $3' | cut -f1 -d"." | sed ':a;N;$!ba;s/n/ /g')

            total=$#IMG_NAME[*]

            for (( i=0; i<$(( $total )); i++ ))
            do
            kubectl set image deployment/$IMG_NAME[$i] $IMG_NAME[$i]=$IMG_ARRAY[$i] --record
            kubectl rollout status deployment/$IMG_NAME[i]
            done


            See https://www.cyberciti.biz/faq/bash-iterate-array/ and https://mywiki.wooledge.org/BashFAQ/005 for explanations.



            Instead of



            total=$#IMG_NAME[*]

            for (( i=0; i<$(( $total )); i++ ))


            you can also use



            for i in $!IMG_NAME[@]


            see https://stackoverflow.com/questions/6723426/looping-over-arrays-printing-both-index-and-value






            share|improve this answer

























            • You could also use array indirection to loop over the indices directly for i in $!arr[@] I think

              – steeldriver
              Jan 17 at 13:02















            1














            My understanding is that you want to have the processed output of docker images in two arrays where every array element corresponds to a line of the processed output.



            This script is untested because I neither know the output of docker images nor do I know the command syntax for kubectl.



            mapfile -t IMG_ARRAY < <(docker images | awk '/dev2/ && /latest/' | awk 'print $1' | sed ':a;N;$!ba;s/n/ /g')
            mapfile -t IMG_NAME < <(docker images | awk '/dev2/ && /latest/' | awk 'print $1' | awk -F'/' 'print $3' | cut -f1 -d"." | sed ':a;N;$!ba;s/n/ /g')

            total=$#IMG_NAME[*]

            for (( i=0; i<$(( $total )); i++ ))
            do
            kubectl set image deployment/$IMG_NAME[$i] $IMG_NAME[$i]=$IMG_ARRAY[$i] --record
            kubectl rollout status deployment/$IMG_NAME[i]
            done


            See https://www.cyberciti.biz/faq/bash-iterate-array/ and https://mywiki.wooledge.org/BashFAQ/005 for explanations.



            Instead of



            total=$#IMG_NAME[*]

            for (( i=0; i<$(( $total )); i++ ))


            you can also use



            for i in $!IMG_NAME[@]


            see https://stackoverflow.com/questions/6723426/looping-over-arrays-printing-both-index-and-value






            share|improve this answer

























            • You could also use array indirection to loop over the indices directly for i in $!arr[@] I think

              – steeldriver
              Jan 17 at 13:02













            1












            1








            1







            My understanding is that you want to have the processed output of docker images in two arrays where every array element corresponds to a line of the processed output.



            This script is untested because I neither know the output of docker images nor do I know the command syntax for kubectl.



            mapfile -t IMG_ARRAY < <(docker images | awk '/dev2/ && /latest/' | awk 'print $1' | sed ':a;N;$!ba;s/n/ /g')
            mapfile -t IMG_NAME < <(docker images | awk '/dev2/ && /latest/' | awk 'print $1' | awk -F'/' 'print $3' | cut -f1 -d"." | sed ':a;N;$!ba;s/n/ /g')

            total=$#IMG_NAME[*]

            for (( i=0; i<$(( $total )); i++ ))
            do
            kubectl set image deployment/$IMG_NAME[$i] $IMG_NAME[$i]=$IMG_ARRAY[$i] --record
            kubectl rollout status deployment/$IMG_NAME[i]
            done


            See https://www.cyberciti.biz/faq/bash-iterate-array/ and https://mywiki.wooledge.org/BashFAQ/005 for explanations.



            Instead of



            total=$#IMG_NAME[*]

            for (( i=0; i<$(( $total )); i++ ))


            you can also use



            for i in $!IMG_NAME[@]


            see https://stackoverflow.com/questions/6723426/looping-over-arrays-printing-both-index-and-value






            share|improve this answer















            My understanding is that you want to have the processed output of docker images in two arrays where every array element corresponds to a line of the processed output.



            This script is untested because I neither know the output of docker images nor do I know the command syntax for kubectl.



            mapfile -t IMG_ARRAY < <(docker images | awk '/dev2/ && /latest/' | awk 'print $1' | sed ':a;N;$!ba;s/n/ /g')
            mapfile -t IMG_NAME < <(docker images | awk '/dev2/ && /latest/' | awk 'print $1' | awk -F'/' 'print $3' | cut -f1 -d"." | sed ':a;N;$!ba;s/n/ /g')

            total=$#IMG_NAME[*]

            for (( i=0; i<$(( $total )); i++ ))
            do
            kubectl set image deployment/$IMG_NAME[$i] $IMG_NAME[$i]=$IMG_ARRAY[$i] --record
            kubectl rollout status deployment/$IMG_NAME[i]
            done


            See https://www.cyberciti.biz/faq/bash-iterate-array/ and https://mywiki.wooledge.org/BashFAQ/005 for explanations.



            Instead of



            total=$#IMG_NAME[*]

            for (( i=0; i<$(( $total )); i++ ))


            you can also use



            for i in $!IMG_NAME[@]


            see https://stackoverflow.com/questions/6723426/looping-over-arrays-printing-both-index-and-value







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 17 at 13:18

























            answered Jan 17 at 12:35









            BodoBodo

            75317




            75317












            • You could also use array indirection to loop over the indices directly for i in $!arr[@] I think

              – steeldriver
              Jan 17 at 13:02

















            • You could also use array indirection to loop over the indices directly for i in $!arr[@] I think

              – steeldriver
              Jan 17 at 13:02
















            You could also use array indirection to loop over the indices directly for i in $!arr[@] I think

            – steeldriver
            Jan 17 at 13:02





            You could also use array indirection to loop over the indices directly for i in $!arr[@] I think

            – steeldriver
            Jan 17 at 13:02

















            draft saved

            draft discarded
















































            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%2f495032%2fbash-array-script-combine%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

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)