3

I'm creating a simple command to execute within GitHub Actions, which builds a Makefile and returns an error if found (matching Error 1) and success when not found...

I seem to either get it to fail even though there is nothing wrong, or not fail when there is something wrong.

Here is my whole command:

find . -name "Makefile" -exec sh -c 'cd "$(dirname "$0")" && make' {} \; | grep -zqvE "Error 1"

I want it to exit 1 if it finds an output like this:

ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [compile] Error 1

Or 0 otherwise

1 Answer 1

6
! find ...as in the question... | grep -qF 'Error 1'

The initial ! causes the pipeline's exit status to be negated. The exit status of the pipeline is the exit status of the grep command, which is zero if the string Error 1 is found in the output of the find command.

Thus, if Error 1 is found in the output, grep exits with zero exit status, and the ! inverts that to a non-zero value.

Note that make may write diagnostic messages to its standard error stream, in which case you have to redirect the output with 2>&1:

! find ...as in the question... 2>&1 | grep -qF 'Error 1'

Also note that grep will terminate as soon as it finds a match, causing find to terminate due to receiving a SIGPIPE signal as soon as it tries to write to the dead pipe.


You tried to use grep with -v. You can think of -v as the question "Are there lines not matching this pattern?". In this case, there will be lines not matching Error 1, regardless of whether Error 1 occurs.

2
  • Yeah ok I see what you mean, but now when it errors it still returns true. When all is OK, nothing is logged, which is to be expected, but when it errors, I get the Makefile make: *** [compile] Error 1 message, but a status of 0 Feb 21, 2023 at 12:43
  • 1
    Ah all good, I had to add 2>&1 for it to work. Cheers :) Feb 21, 2023 at 12:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .