1

My question is relative to Ubuntu 20.04 where I am performing these tasks.

I currently pass some state information to a debug trap such as $BASH_COMMAND, $LINENO and $?. I would like to pass any standard error message generated by the command to a trap function too. $? is probably not valid because it would be the return code of the prior instruction not the current instruction as the current instruction has not executed yet. $? and the stderr message should be probably be passed to the ERR trap function after execution of the command. The following link started a thought process about the question: Can I redirect 2> to a variable in a generic fashion for every instruction executed?

How to capture error message from executed command?

If so I can pass the variable that contains the last stderr message to my ERR trap function too. Unfortunately I am at a loss as to how to perform that redirection to a variable in such a manner as to be able to have the command, line number, return code and standard error message for the last command all in one place at the same time to call the trap function.

Perhaps there is a way redirect stderr to a non-appended file uniquely names using the subshell, source, function name, and line number (command too maybe) where that file could be read after the trap function initiates. In this case it would be good to have these files written to memory and purged in an automated manner instead of to disk (SSD or RUST). Creating + deleting these files would result in considerable overhead.

Considering the negatives of all the above do you think those controlling the addition bash special variables would consider a new variable possibly $% related to $? to hold the stderr message to pass to probably the ERR trap be a better answer? Maybe there is already such a variable and I have not found the name of it yet

Thanks

a skeleton of the debug trap

    fxTrapDEBUG()
    {
        set -x
        local lBashCommand="$1"
        local lLineNo="$2"
        set +x
    }
trap 'fxTrapDEBUG "$BASH_COMMAND" "$LINENO"' DEBUG

a skeleton for the ERR (error) trap

    fxTrapERROR()
    {
        set -x
        local lBashCommand="$1"
        local lLineNo="$2"
        local lReturnCode="$3"
        local lStdErrMsg="$4"
        set +x
    }
trap 'fxTrapERROR "$BASH_COMMAND" "$LINENO" "$?" "???"' ERR

My question is what do i put in place of "???" above.

0 Answers0