Bash 迭代输出的列

Bash 迭代输出的列,bash,Bash,我试图迭代kubernetes名称空间,以便打印出每个名称空间中每个入口的ip地址 这是非常奇怪的。在我的shell中运行命令可以工作,但运行脚本不起作用 基本上,NAMESPACES=($(echo$NAMESPACES))仅在从脚本运行时获取输出中的第一项,而在从我的shell运行时,它正确地获取输出的每一行。因此,当for循环运行时,它只对一个项进行迭代 我在mac电脑上 有人能确定我应该如何迭代名称空间列吗?我在脚本之后列出输出。谢谢 #!/bin/bash # gcloud cont

我试图迭代kubernetes名称空间,以便打印出每个名称空间中每个入口的ip地址

这是非常奇怪的。在我的shell中运行命令可以工作,但运行脚本不起作用

基本上,
NAMESPACES=($(echo$NAMESPACES))
仅在从脚本运行时获取输出中的第一项,而在从我的shell运行时,它正确地获取输出的每一行。因此,当for循环运行时,它只对一个项进行迭代

我在mac电脑上

有人能确定我应该如何迭代名称空间列吗?我在脚本之后列出输出。谢谢

#!/bin/bash

# gcloud container clusters get-credentials $CLUSTER --zone $ZONE --project $PROJECT

# get just the column of namespace output
NAMESPACES=$(kubectl get namespace | awk '{print $1}' | tail -n +2)

# transpose the column into an array
NAMESPACES=( $( echo $NAMESPACES ) )

echo
echo "Printing out feature branch deployments and corresponding ip addresses:"
echo

for ns in $NAMESPACES
do
  # check if feature is in the namespace
  if [[ $ns == "feature-"* ]]
  then
    IP_ADDRESS=$(kubectl get ingress --namespace $ns | grep $ns | awk '{print $3}')
    echo $ns 'ip address:' $IP_ADDRESS
  fi
done
下面是$namespace的外观

$ echo $NAMESPACES
chartmuseum 
default 
feature-1
feature-2 
feature-3
feature-4
feature-5
kube-public 
kube-system
qa
twistlock

# here's what $NAMESPACES looks like after it's transposed into the array
# this is what is iterated over in the for loop
$ echo $NAMESPACES
chartmuseum 
default feature-1 feature-2 feature-3 feature-4 feature-5 kube-public  kube-system qa twistlock

编辑-答案如下:只需注释掉
名称空间=($(echo$NAMESPACES))
,脚本就可以运行了。感谢@bigdataolddriver

尝试使用以下语法引用数组:

for ns in "${NAMESPACES[@]}"
do

done

注释出转置行。不要担心for循环。这很有效!我发誓我曾经试过这样做?不管怎样,谢谢你!!