You need to enable JavaScript to run this app. AWS EKS CLI编写的shell脚本if/else分支不生效问题求助
优惠活动
大模型
产品
解决方案
定价
更多

AWS EKS CLI编写的shell脚本if/else分支不生效问题求助

AWS EKS集群存在性检测脚本异常问题修复

问题根因

  • 核心原因是脚本大概率开启了 set -e 配置:该配置会让脚本遇到任意返回非0退出码的命令时直接终止运行。当目标集群不存在时,grep 匹配失败返回退出码1,触发终止逻辑,脚本直接退出,不会执行后续的if/else分支,因此无任何输出。
  • 现有代码的jq逻辑不严谨:jq -r ".clusters" 输出的是JSON数组整体而非每行一个集群名,容易出现误匹配。

修复方案

推荐直接将匹配逻辑放入if判断条件中,既可以规避set -e的误终止,逻辑也更简洁:

if cluster=$(aws eks list-clusters | jq -r '.clusters[]' | grep "mycluster_test"); then
    echo "this worked, the cluster is $cluster"
else
    echo "no cluster by this name"
fi

也可以用grep -q先做静默判断,不需要提前捕获集群名:

aws eks list-clusters | jq -r '.clusters[]' | grep -q "mycluster_test"
if [ $? -eq 0 ]; then
    echo "this worked, the cluster is mycluster_test"
else
    echo "no cluster by this name"
fi

内容的提问来源于stack exchange,提问作者scrow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 09:54:06