简体   繁体   English

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

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

I'm trying to do a basic if/else that checks to see if a cluster already exists before creating another of that name.我正在尝试做一个基本的 if/else 来检查集群是否已经存在,然后再创建另一个该名称。 This is the entire section;这是整个部分;

 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

There is no cluster with this name, and when I run the script it returns nothing.没有同名的集群,当我运行脚本时它什么也不返回。 But I don't understand why it's not returning the else statement但我不明白为什么它不返回 else 语句

I do have a cluster called 'mycluster_new' and when I grep for this, the first echo statement is returned, so can I please get help on why the else statement is failing.我确实有一个名为“mycluster_new”的集群,当我为此使用grep时,会返回第一个 echo 语句,所以我能否就else语句失败的原因获得帮助。 Thanks谢谢

try this尝试这个

if your vairabe is string如果你的 vairabe 是字符串

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

if your variable is integer如果您的变量是 integer

if [[ $cluster -eq 1 ]]; then

Managed to resolve this by checking if the string was empty and ran a check that would continue regardless of if it was empty or not.设法通过检查字符串是否为空来解决此问题,并运行检查将继续进行,无论它是否为空。

CLUSTER=my_eks_cluster

CHECK_NAME=$(aws eks list-clusters | jq -r ".clusters" | grep $CLUSTER || true)

then ran a check for this;然后对此进行检查;

    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

if you really want to go ahead with your old way, you can always use '-z' to check if the string is null如果你真的想用你的旧方法提前 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