cant suppress or use an AWS-CLI error in bash - bash

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.

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.

Related

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. 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
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. Thanks
try this
if your vairabe is string
if [[ $cluster == 1 ]]; then
if your variable is 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
if [ -z "$cluster" ]; then
echo "this worked, the cluster is $cluster"
else
echo "no cluster by this name"
fi

using cronic to reduce email notifications in a wrapper script for successful backups

I've been using cronic to silence emails from cron jobs when the job is successful. I'm trying to customize it so when a response code is 0 and the error output matches a string of "mount: /VessRAID/RH: /dev/sde1 already mounted on /VessRAID/RH.", to not send an email. Below is the script, then the contents of the email then my attempt at trying to suppress the email which is not working. Any idea what I may be doing wrong?
#!/bin/bash
# Cronic v3 - cron job report wrapper
# Copyright 2007-2016 Chuck Houpt. No rights reserved, whatsoever.
# Public Domain CC0: http://creativecommons.org/publicdomain/zero/1.0/
set -eu
TMP=$(mktemp -d)
OUT=$TMP/cronic.out
ERR=$TMP/cronic.err
TRACE=$TMP/cronic.trace
set +e
"$#" >$OUT 2>$TRACE
RESULT=$?
set -e
PATTERN="^${PS4:0:1}\\+${PS4:1}"
if grep -aq "$PATTERN" $TRACE
then
! grep -av "$PATTERN" $TRACE > $ERR
else
ERR=$TRACE
fi
if [ $RESULT -ne 0 -o -s "$ERR" ]
then
echo "Cronic detected failure or error output for the command:"
echo "$#"
echo
echo "RESULT CODE: $RESULT"
echo
echo "ERROR OUTPUT:"
cat "$ERR"
echo
echo "STANDARD OUTPUT:"
cat "$OUT"
if [ $TRACE != $ERR ]
then
echo
echo "TRACE-ERROR OUTPUT:"
cat "$TRACE"
fi
fi
rm -rf "$TMP"
Here is what the email notification looks like:
Cronic detected failure or error output for the command:
/usr/local/sbin/reg-backup-cronic.sh daily
RESULT CODE: 0
ERROR OUTPUT:
mount: /VessRAID/RH: /dev/sde1 already mounted on /VessRAID/RH.
STANDARD OUTPUT:
/dev/sde1 on /VessRAID/RH type ext4 (rw,relatime)
Here is my attempt at a wrapper script:
#!/bin/bash
/usr/local/sbin/reg-backup.sh $1
CODE=$?
err=$TRACE
if [[ $CODE -eq 0 && $err = "mount: /VessRAID/RH: /dev/sde1 already mounted on /VessRAID/RH." ]]
then
exit $CODE
fi
Alas the emails continue.
Hat tip to the creator of cronic, Chuck Houpt, for cluing me in to an answer, which was to look at the original script and why the error is happening. Case-sensitivity got the best of me:
if mount | grep Vessraid; then
echo starting $1 backup >> /var/log/vessraid.log
Notice the case in VessRAID should have been:
if mount | grep VessRAID; then
echo starting $1 backup >> /var/log/vessraid.log
Now emails only happen when there really is an error.

Shell Script won't fail in Jenkins

I have a simple shell script which I want to set up as a periodic Jenkins job rather than a cronjob for visibility and usability for less experienced users.
Here is the script:
#!/bin/bash
outputfile=/opt/jhc/streaming/check_error_output.txt
if [ "grep -sq 'Unable' $outputfile" == "0" ]; then
echo -e "ERROR MESSAGE FOUND\n"
exit 1
else
echo -e "NO ERROR MESSAGES HAVE BEEN FOUND\n"
exit 0
fi
My script will always return "NO ERROR MESSAGES HAVE BEEN FOUND" regardless of whether or not 'Unable' is in $outputfile, what am I doing wrong?
I also need my Jenkins job to class this as a success if 'Unable' isn't found (e.g. If script returns "0" then pass, everything else is fail)
Execute the grep command and check the exit status instead:
#!/bin/bash
outputfile=/opt/jhc/streaming/check_error_output.txt
grep -sq 'Unable' $outputfile
if [ "$?" == "0" ]; then
echo -e "ERROR MESSAGE FOUND\n"
exit 1
else
echo -e "NO ERROR MESSAGES HAVE BEEN FOUND\n"
exit 0
fi
You are comparing two different strings. The outcome will always be false, i.e. the else part is taken.
Also, no need to explicitly query the status code. Do it like this:
if grep -sq 'Unable' $outputfile
then
....
else
....
fi

Bash sub script redirects input to /dev/null mistakenly

I'm working on a script to automate the creation of a .gitconfig file.
This is my main script that calls a function which in turn execute another file.
dotfile.sh
COMMAND_NAME=$1
shift
ARG_NAME=$#
set +a
fail() {
echo "";
printf "\r[${RED}FAIL${RESET}] $1\n";
echo "";
exit 1;
}
set -a
sub_setup() {
info "This may overwrite existing files in your computer. Are you sure? (y/n)";
read -p "" -n 1;
echo "";
if [[ $REPLY =~ ^[Yy]$ ]]; then
for ARG in $ARG_NAME; do
local SCRIPT="~/dotfiles/setup/${ARG}.sh";
[ -f "$SCRIPT" ] && echo "Applying '$ARG'" && . "$SCRIPT" || fail "Unable to find script '$ARG'";
done;
fi;
}
case $COMMAND_NAME in
"" | "-h" | "--help")
sub_help;
;;
*)
CMD=${COMMAND_NAME/*-/}
sub_${CMD} $ARG_NAME 2> /dev/null;
if [ $? = 127 ]; then
fail "'$CMD' is not a known command or has errors.";
fi;
;;
esac;
git.sh
git_config() {
if [ ! -f "~/dotfiles/git/gitconfig_template" ]; then
fail "No gitconfig_template file found in ~/dotfiles/git/";
elif [ -f "~/dotfiles/.gitconfig" ]; then
fail ".gitconfig already exists. Delete the file and retry.";
else
echo "Setting up .gitconfig";
GIT_CREDENTIAL="cache"
[ "$(uname -s)" == "Darwin" ] && GIT_CREDENTIAL="osxkeychain";
user " - What is your GitHub author name?";
read -e GIT_AUTHORNAME;
user " - What is your GitHub author email?";
read -e GIT_AUTHOREMAIL;
user " - What is your GitHub username?";
read -e GIT_USERNAME;
if sed -e "s/AUTHORNAME/$GIT_AUTHORNAME/g" \
-e "s/AUTHOREMAIL/$GIT_AUTHOREMAIL/g" \
-e "s/USERNAME/$GIT_USERNAME/g" \
-e "s/GIT_CREDENTIAL_HELPER/$GIT_CREDENTIAL/g" \
"~/dotfiles/git/gitconfig_template" > "~/dotfiles/.gitconfig"; then
success ".gitconfig has been setup";
else
fail ".gitconfig has not been setup";
fi;
fi;
}
git_config
In the console
$ ./dotfile.sh --setup git
[ ?? ] This may overwrite existing files in your computer. Are you sure? (y/n)
y
Applying 'git'
Setting up .gitconfig
[ .. ] - What is your GitHub author name?
Then I cannot see what I'm typing...
At the bottom of dotfile.sh, I redirect any error that occurs during my function call to /dev/null. But I should normally see what I'm typing. If I remove 2> /dev/null from this line sub_${CMD} $ARG_NAME 2> /dev/null;, it works!! But I don't understand why.
I need this line to prevent my script to echo an error in case my command doesn't exists. I only want my own message.
e.g.
$ ./dotfile --blahblah
./dotfiles: line 153: sub_blahblah: command not found
[FAIL] 'blahblah' is not a known command or has errors
I really don't understand why the input in my sub script is redirected to /dev/null as I mentioned only stderr to be redirected to /dev/null.
Thanks
Do you need the -e option in your read statements?
I did a quick test in an interactive shell. The following command does not echo characters :
read -e TEST 2>/dev/null
The following does echo the characters
read TEST 2>/dev/null

Get cURL response in bash

I have a simple bash script that uploads files to an FTP. I was wondering how to get a response from curl that I can record (error or success)?
eval curl -T "${xmlFolder}"/"${xmlFile}" "${mediaFTP}"
Thanks in advance
Given the command provided, this should suffice:
curl -T "$xmlFolder/$xmlFile" "$mediaFTP" ||
printf '%s\n' $?
Or, if you want to discard the error message:
curl -T "$xmlFolder/$xmlFile" "$mediaFTP" >/dev/null ||
printf '%s\n' $?
The $? bash variable indicates success (val 0) / failure (val non 0) of the previous command. So you could do:
eval curl -T "${xmlFolder}"/"${xmlFile}" "${mediaFTP}"
err=$?
if [ $err -ne 0 ]
then
echo "Failed with error code $err"
exit
fi

Resources