Loading...

cant suppress or use an AWS-CLI error in bash

Answer #1 100 %

Here's a consideration, and expansion on my comments. Again you're getting a stderr response when no cluster is found, so this makes this pretty straightforward.

Using >2 /dev/null to suppress that return message in stderr. Then using ret=$? to capture the return code.

get_cluster_name() {
  EKS_NAME=$(aws eks describe-cluster --name ${CUSTOMER_NAME} 2> /dev/null);
  ret=$?
  if [ $ret -eq 0 ]; then
     echo $EKS_NAME | jq -r .cluster.name
     return 0
  else
     return $ret
  fi
}

You can do the same thing now when you call the function as your error will propagate from aws command up the stack:

cluster=$(get_cluster_name)
ret=$?
if [ $ret -eq 0 ]; then
  echo $cluster
else
  echo "failed to find cluster. Error code: $ret"
fi

As an example.

You’ll also like:


© 2023 CodeForDev.com -