简体   繁体   中英

Array Concatenation in Shell script

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM