【问题标题】:Inverted exit code running lint command in a Makefile在 Makefile 中运行 lint 命令的反向退出代码
【发布时间】:2016-02-27 19:52:56
【问题描述】:

我的 Makefile 中的这个条目很高兴地爬取我的 PHP 文件并在它们上运行 PHP 的内置 lint:

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

grep -v 抑制所有“未检测到语法错误”消息,否则这些消息会在仍然失败消息(如果有)时生成。

问题是 make 在没有语法错误时会死掉,而在有错误时会继续。这是因为来自grep -v 的退出代码。它认为当它找到某些东西时它已经成功(一条错误消息),当它什么也没找到时它认为它失败了(所有文件都通过了 lint)。

我查看了用! 否定最终调用 grep 的退出代码:

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

但这给了我:

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

我可以在命令行中正常使用!,但在这种情况下由于某种原因它不起作用。

我很好奇我如何在 pipeline/xargs/grep/make 的上下文中否定退出代码。但大多数情况下,我想解决我的问题 - 接受任何会导致我的 Makefile 中的 lint 目标正常工作的建议。

【问题讨论】:

标签: php bash command-line makefile


【解决方案1】:

管道的返回值是它的最后一条命令返回的值。所以你只需要恢复完整命令行的状态:

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

【讨论】:

  • 啊,太简单了!谢谢!
猜你喜欢
  • 2014-06-14
  • 2021-04-24
  • 1970-01-01
  • 2019-08-28
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多