Is it possible to get the error message from previous command which failed when the conditional command runs using ||
Solution 1
You can redirect the error output to a file and then retrieve that output:
trap "rm -f /tmp/cfn-error.txt" 0 1 2 3 15
/opt/aws/bin/cfn-init -s ... 2>/tmp/cfn-error.txt ||
error_exit $(</tmp/cfn-error.txt)
You should always clean up your mess, so don't forget to delete any temp files you create.
Solution 2
You can also do this by redirection:
# run_cmd 'error message' cmd -foo "${params[@]}"
run_cmd() {
local e r m=$1
shift
exec 6>&1
e=$("$@" 2>&1 >&6)
r=$?
exec 6>&-
((r)) || return 0
error_exit "$m" "$e"
}
So you would use:
run_cmd 'Failed to run cfn-init' /opt/aws/bin/cfn-init -s ..
The line: e=$("$@" 2>&1 >&6) first directs stderr to stdout, which in the context of $(..) is the output we're capturing. Then stdout is directed to where it originally went when we started the function.
Of course, you can make error_exit additionally take the exit status, and call it with eg: error_exit "$m" "$r" "$e"
Related videos on Youtube
Ribbo
Been working on both .NET and Java and often have a diversity of roles in projects like technical lead, developer, sysadmin and more. Lately I've worked on a WPF application with a Java back end server running on Amazon EC2.
Updated on September 18, 2022Comments
-
Ribbo over 1 year
I have a helper function:
function error_exit { /opt/aws/bin/cfn-signal ... exit 1 }
This helper function is used to signal an error. Here is an example of usage:
/opt/aws/bin/cfn-init -s .. || error_exit 'Failed to run cfn-init'
The
cfn-init
command takes a lot of parameters which isn't relevant for the question. When the command returns a non-null value and possibly an error message to the error output, I would like to get the error message and include it to theerror_exit
method as a parameter. Is this possible? If not, how would you implement a helper method in bash which makes it possible to get the source error message?-
Admin almost 12 yearsIn the context of CloudFormation's stock templates, from which this looks to derive, also consider setting 'DisableRollback' with
--disable-rollback
in the CLI tools. Also usecfn-init
's-v
flag to get verbose output.
-
-
Aquarius Power almost 10 yearsany idea how to make it handle unbound variables
set -u;echo $str
? -
Arcege almost 10 yearsDo you mean handle when the shell would exit because of the
-u
option? In (da)sh, there istrap 0
. In bash, there istrap ERR
. -
Aquarius Power almost 10 yearsI was working on this answer/question, yep I ended having to use
trap ERR