Shell脚本中的数组串联

问题描述 投票:1回答:1

我尝试了一个简单的shell脚本来查看statefulset的图像,然后根据需要替换它们。

我遇到了一个问题,即存储的数组值无法正确显示。这是脚本

#!/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

所需的输出应该是

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

我得到的输出是

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:

当我这样做时,echo $ {imgver [@]}“

它返回此

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

我相信串联声明的数组是这里的麻烦。帮助我在这里找到问题,或者让我知道是否还有其他方法来实现所需的输出。

arrays bash shell for-loop concat
1个回答
1
投票

感谢@socowi,我弄清楚了这个问题。当执行clarify -p时,它列出了变量。由于存在空格,因此字符串image:也被当作一个单独的变量。我选择了awk而不是egrep,并且能够实现所需的输出。

© www.soinside.com 2019 - 2024. All rights reserved.