Bash array script, combine

Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
It would be easier to help if you would show the output ofdocker imagesas 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 ofdocker imageswithout your post-processing. I want to see the output lines, not what you have combined into variablesIMG_NAMEandIMG_ARRAY
– Bodo
Jan 17 at 12:59
add a comment |
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
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
bash array
asked Jan 17 at 11:37
user37033user37033
191
191
It would be easier to help if you would show the output ofdocker imagesas 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 ofdocker imageswithout your post-processing. I want to see the output lines, not what you have combined into variablesIMG_NAMEandIMG_ARRAY
– Bodo
Jan 17 at 12:59
add a comment |
It would be easier to help if you would show the output ofdocker imagesas 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 ofdocker imageswithout your post-processing. I want to see the output lines, not what you have combined into variablesIMG_NAMEandIMG_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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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
You could also use array indirection to loop over the indices directlyfor i in $!arr[@]I think
– steeldriver
Jan 17 at 13:02
add a comment |
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
);
);
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
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 17 at 13:17
ilkkachuilkkachu
57.7k888163
57.7k888163
add a comment |
add a comment |
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
You could also use array indirection to loop over the indices directlyfor i in $!arr[@]I think
– steeldriver
Jan 17 at 13:02
add a comment |
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
You could also use array indirection to loop over the indices directlyfor i in $!arr[@]I think
– steeldriver
Jan 17 at 13:02
add a comment |
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
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
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 directlyfor i in $!arr[@]I think
– steeldriver
Jan 17 at 13:02
add a comment |
You could also use array indirection to loop over the indices directlyfor 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
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
It would be easier to help if you would show the output of
docker imagesas 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 imageswithout your post-processing. I want to see the output lines, not what you have combined into variablesIMG_NAMEandIMG_ARRAY– Bodo
Jan 17 at 12:59