Is it possible to get the error message from previous command which failed when the conditional command runs using ||

7,098

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"

Share:
7,098

Related videos on Youtube

Ribbo
Author by

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, 2022

Comments

  • Ribbo
    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 the error_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
      Admin almost 12 years
      In 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 use cfn-init's -v flag to get verbose output.
  • Aquarius Power
    Aquarius Power almost 10 years
    any idea how to make it handle unbound variables set -u;echo $str ?
  • Arcege
    Arcege almost 10 years
    Do you mean handle when the shell would exit because of the -u option? In (da)sh, there is trap 0. In bash, there is trap ERR.
  • Aquarius Power
    Aquarius Power almost 10 years
    I was working on this answer/question, yep I ended having to use trap ERR