簡體   English   中英

AWS EKS CLI 命令 - 如果/否則不起作用

[英]AWS EKS CLI command - if/else not working

我正在嘗試做一個基本的 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

沒有同名的集群,當我運行腳本時它什么也不返回。 但我不明白為什么它不返回 else 語句

我確實有一個名為“mycluster_new”的集群,當我為此使用grep時,會返回第一個 echo 語句,所以我能否就else語句失敗的原因獲得幫助。 謝謝

嘗試這個

如果你的 vairabe 是字符串

if [[ $cluster == 1 ]]; then

如果您的變量是 integer

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

如果你真的想用你的舊方法提前 go,你總是可以使用'-z'來檢查字符串是否是 null

 if [ -z "$cluster" ]; then
     echo "this worked, the cluster is $cluster"
 else
     echo "no cluster by this name"
 fi

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM