Failing at grep

I have gitlab pipeline, where at the end of execution, there is a text file generated.
In that, I am trying to search for strings with Error and want to show the job as failed in the pipeline.

So. I added below part of code at the end.


.check_remote_log: &check_remote_log

- ls -la

- returnCode=$(grep -c "Error:" outfile)

- echo "Return code received $returnCode"

- if [ $returnCode -ge 1 ]; then exit 1; fi

And calling this at the end of steps as

    - echo "Now checking log file for returnCode"
    - *check_remote_log

What I observed in the output is, the file is getting generated as I can see in the ls command.
But it is failing at the grep step.

image

But these commands are working when I ran individually on a linux machine.

Even I tried this:

if [[ $(grep "Error" outfile) ]] ; then exit 1; else echo "No errors in outfile"; fi

But getting below error.

When there is error text also in the file, still getting below output.

Please suggest.