Exit 1 doesn't work

Hi

tail -f $PROGPATH/NBU_pgbaserestore_$1.log | while read LOGLINE
        do
        if [[ $LOGLINE =~ .*INF\ -\ Status.* ]] && ! [[ $LOGLINE =~ .*success.* ]]
        then
        date "+%d.%B.%Y %H:%M:%S"
        echo "ERROR: NBU"
        echo "$LOGLINE"
        TAILKILL=$(pgrep -P $$ -x tail)
        kill -9 $TAILKILL
        exit 1
        elif [[ $LOGLINE =~ .*INF\ -\ Status.*success.* ]]
        then
        date "+%d.%B.%Y %H:%M:%S"
        echo "NBU: $LOGLINE"
        TAILKILL=$(pgrep -P $$ -x tail)
        kill -9 $TAILKILL
        fi
        done

And exit 1 there will be skipped. Why?

This is because the exit command is executed in a subshell (the right hand side of the pipeline). So the subshell is exited, but not the parent shell.

If you were to use ksh (Korn Shell) or zsh , then it would work because these shells do not execute the RHS of a pipeline in a subshell (but rather in the foreground)

--
In bash you could try adding this after the done statement:

RC=$?
if [ $RC -ne 0 ]; then
 exit $RC
fi
1 Like

Your solution works, thnx

Or simply

...
done || exit
1 Like