6

i have a simple script that i want to display specific information from AWS using AWS CLI.

for example:

get_cluster_name() {
EKS_NAME=$(aws eks describe-cluster --name ${CUSTOMER_NAME}) && \
echo $EKS_NAME | jq -r .cluster.name}

the output when the cluster exist is ok, i get the name of the cluster.

when the cluster does not exist, i get:

An error occurred (ResourceNotFoundException) when calling the DescribeCluster operation: No cluster found for name: example_cluster.

my goal is to get an empty output when a cluster is not found. for that i wanted to use the return code in a condition or a string lookup in the output.

the issue is that the output is not stdout or stderr, therefor i cant even direct it to /dev/null just to silence the error.

how can i make this code work properly?:

[[ $(get_cluster_name) =~ "ResourceNotFoundException" ]] && echo "EKS Cluster:....$(get_cluster_name)"

or

[[ $(get_cluster_name) ]] && echo "EKS Cluster:....$(get_cluster_name)"

Thank you.

itsme
  • 95
  • 2
  • 8
  • 1
    It is, in fact, stderr. I just tried `aws eks describe-cluster --name "test" 2> eks_output`. Nothing appeared in my terminal and file `eks_output` contained your error code: `An error occurred (ResourceNotFoundException) when calling the DescribeCluster operation: No cluster found for name: test.` Also `echo $?` after running, reports a return code of `254`. – JNevill Jul 28 '21 at 13:41
  • 1
    `he issue is that the output is not stdout or stderr` Doubting that, but to confirm: run the command under `strace` and trace the `write` call that prints this message and track the file descriptor this message is written to. – KamilCuk Jul 28 '21 at 13:47
  • 1
    Being that it's a stderr response, if you just want to suppress the error you could do: `EKS_NAME=$(aws eks describe-cluster --name ${CUSTOMER_NAME} >2 /dev/null)`. This way your `EKS_NAME` variable is either empty or is filled with the json response if it was successful. – JNevill Jul 28 '21 at 13:51
  • 2>/dev/null did the trick. i dont know how i missed it... sorry and thank you guys. – itsme Jul 28 '21 at 13:59
  • Does this answer your question? [How do I suppress shell script error messages?](https://stackoverflow.com/questions/15678796/how-do-i-suppress-shell-script-error-messages) – Aserre Jul 28 '21 at 14:02

1 Answers1

10

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.

JNevill
  • 46,980
  • 4
  • 38
  • 63
  • That does not work with `(AccessDeniedException)` errors. No matter what I tried, I was not able to suppress `(AccessDeniedException)` `aws cli` errors in bash. – t7e May 19 '22 at 23:23
  • That's odd. It seems very unlikely that awscli would be open it's own filedescriptors besides using the built in stderr and stdout. can you try catching `3>` and seeing if that's the hole that `(AccessDeniedException)` is spitting out from? – JNevill May 20 '22 at 13:55
  • My bad. I did some piping and `2>/dev/null` should have been put right after the `aws` command before the pipe and not at the end of the whole pipe. – t7e May 20 '22 at 20:48