Pipe causing last command error to not function

Hi

I am quite new to scripting and cannot work out how to do the following -

I want to pipe to a log file and then use the "last statement error" in an if statement after, and this doesn't work because it checks the pipe statement instead of the script.

Example:

executteTheScript $var | tee $LOG
if[[ $? != 0 ]]
...

So even if executeTheScript fails, the if statement doesn't pick it up because tee passes - does anyone know how to solve this issue?

Thanks

Does the script log any text that could be used as a pass/fail flag?

You could also break it up:

if ! executteTheScript $var
then
        cat $log
        handleerror
else
        cat $log
fi

First of all, did you know that your log will only contain the standard output stream (stdout)?

Use yourscript $var 2>&1 | tee $LOG to capture the standard error (stderr) stream too. This might help you with the potential troubleshooting process.

In bash, there is a special internal array variable called $PIPESTATUS, which may work relatively reliable for the simple case you mention.

${PIPESTATUS[0]} should hold the exit status of the first command in the pipe.

Source: Internal Variables

1 Like

Thanks guys

I fixed it by splitting the two commands - there was probably a better was to do it, but it works and that's all I need right now!