0

I am new to bash shell scripting and facing a problem.

I have code like this:

function build()
{

    echo "Building: " | tee -a $LOG_FILE
    { 
        #do some processing here and if error set the err var to say 2
        if [[ $err -eq 2 ]]; then
           echo "Error: exit"
           exitStatus=2
        fi
    } 2>&1 | tee -a $LOG_FILE | grep -e "$GREP_FAIL_TERM" -e "$GREP_ERROR_TERM"
 }

where

 GREP_FAIL_TERM='[Ff]ail'
 GREP_ERROR_TERM='[Ee][Rr][Rr][Oo][Rr][,;:\ ]'

and

 exitStatus is a global variable set to 0 at the beginning of script

When I come out of this function in case of error and check the value of exitStatus it is 0. I read that using | executes the command in subshell which is not reflected in parent. But then, how can I execute the above code while outputting to log file.

1 Answers1

0

Yes, it's a difficult scenario.

The easiest way will be to put markers in the output of the subshell script, and determine status based on their presence.

You do grep for error already (although grep -i would be a good friend to you). The return code of that should indicate errors to you.

function build()
{
    { 
        echo "Building: "
        # do stuff
    } 2>&1 | tee -a $LOG_FILE | grep -Ei "fail|error[,;: ]"
    SUCCESS=$? # 0 if an error was detected
}
Felix Frank
  • 8,125
  • 1
  • 23
  • 30