1

I made an attempt for a simple shell script to view statefulset's images, and then replace them if desired.

I ran into this issue that the array values that are stored are not getting displayed properly.. here is the script

#!/bin/bash
##-chronograph3r
##Variables 

read -p "Namespace : " NS

##### To find the Image tags in statefulset 

app=($(kubectl get statefulset -n $NS -o=name | grep "myapp" | cut -c 18-) )
declare app
imgver=($(kubectl get statefulset -n $NS ${array[@]} -o yaml | egrep "image: asia.gcr.io" ))
declare imgver

#--- To display current image version
for i in $@
do
    app[${#app[@]}]=$i
    imglist[${#imgver[@]}]=$i
    echo $i
done
for  (( i==0; i < ${#app[@]}; i++  ))
do
    echo "current image version for ${app[$i]} is ${imgver[$i]}" 
done

the desired output should be,

current image version for myapp1 is image: asia.gcr.io/lucifer/myapp:latest
current image version for myapp2 is image: asia.gcr.io/lucifer/myapp:1.4.2
current image version for myapp3 is image: asia.gcr.io/lucifer/myapp:1.3.0
current image version for myapp4 is image: asia.gcr.io/lucifer/myapp:stable

the output I get is

current image version for myapp1 is image: asia.gcr.io/lucifer/myapp:latest
current image version for myapp2 is image: 
current image version for myapp3 is image: asia.gcr.io/lucifer/myapp:1.3.0
current image version for myapp4 is image:

When I do " echo ${imgver[@]}"

it returns this

image: asia.gcr.io/lucifer/myapp:latest  image: asia.gcr.io/lucifer/myapp:1.4.2  image: asia.gcr.io/lucifer/myapp:1.3.0  image: asia.gcr.io/lucifer/myapp:stable

I believe that concatenating the declared arrays is the troublemaker here. help me find the issue here or let me know if there are other ways to achieve the desired output.

4
  • 2
    First, quote your variables. Especially ${array[@]} and $@ should be "${array[@]}" and "$@". Also, the declare commands are not necessary here. By the way, app[${#app[@]}]=... could be abbreviated as app+=(...) or even app+=("$@") if you leave out the loop. Commented Dec 13, 2019 at 12:07
  • For debugging, could you give us the output of declare -p imgver? Commented Dec 13, 2019 at 12:11
  • I think I figured the issue. Culprit is the whitespace after the word image: asia.gcr.io/lucifer/myapp . so when stored in array of imgver every element after the white-space is considered as a separate value. hence the difference in output when looping. help me figure this out @Socowi on how to do this. Commented Dec 13, 2019 at 12:25
  • To convert the lines (including spaces) of cmd's output into an array you can use mapfile -t array < <(cmd). Commented Dec 13, 2019 at 12:27

1 Answer 1

1

Thanks to @socowi I figured out the issue. When doing declare -p it listed out the variables. and because of the white spaces, the string image: was also taken as a separate variable. instead of egrep I opted for awk and I was able to achieve the required output.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.