我正在尝试做一个基本的if/else,在创建另一个同名集群之前,检查集群是否已经存在。这是整个部分;
cluster=$(aws eks list-clusters | jq -r ".clusters" | grep mycluster_test)
if [ $? -eq 0 ]; then
echo "this worked, the cluster is $cluster"
else
echo "no cluster by this name"
fi
没有使用此名称的集群,当我运行脚本时,它不会返回任何结果。但我不明白为什么它不返回其他声明
我确实有一个名为"mycluster_new"的集群,当我为此grep
时,会返回第一个echo语句,所以我可以获得关于else
语句失败原因的帮助吗。感谢
尝试这个
如果你的vairabe是字符串
if [[ $cluster == 1 ]]; then
如果您的变量是整数
if [[ $cluster -eq 1 ]]; then
通过检查字符串是否为空来解决此问题,并运行了一个检查,该检查将继续进行,无论它是否为空。
CLUSTER=my_eks_cluster
CHECK_NAME=$(aws eks list-clusters | jq -r ".clusters" | grep $CLUSTER || true)
然后对此进行了检查;
if [ "$CHECK_NAME" != "" ]; then
echo "There is already a cluster by this name; $CHECK_NAME. Cannot build another"
exit 1
else
echo "No cluster by this name $CLUSTER, will continue with terraform"
fi
如果你真的想继续你的旧方法,你可以随时使用'-z'来检查字符串是否为空
if [ -z "$cluster" ]; then
echo "this worked, the cluster is $cluster"
else
echo "no cluster by this name"
fi