简体   繁体   中英

Inverted exit code running lint command in a Makefile

This entry in my Makefile happily crawls my PHP files and runs PHP's built-in lint on them:

lint:
    @find . -name "*.php" | grep -v "^./lib" | grep -v "^./vendor" | xargs -I{} php -l '{}' | grep -v "No syntax errors detected"

The grep -v suppresses all the "No syntax errors detected" messages that would otherwise be produced while still failure messages, if any.

The problem is that make dies when there are no syntax errors and continues when there are errors. This is because of the exit code from grep -v . It thinks it has succeeded when it finds something (an error message) and failed when it finds nothing (all files passed lint).

I looked at negating the exit code of the final call to grep with ! :

lint:
    @find . -name "*.php" | grep -v "^./lib" | grep -v "^./vendor" | xargs -I{} php -l '{}' | ! grep -v "No syntax errors detected"

but that gives me:

/bin/sh: -c: line 0: syntax error near unexpected token `!'

I can use ! at the commandline fine but in this context it doesn't work for some reason.

I'm curious how I negate an exit code within the context of a pipeline/xargs/grep/make. But mostly I want to solve my problem - open to any suggestions that result in a working lint target in my Makefile that does the right thing.

Return value of pipe is one which is returned by its last command . So you need just revert status of full command line:

lint:
    @! find ... | grep -v "No syntax errors detected"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM