"grep 这个 foo.txt >这个.txt”当 grep 输出为空时,在 Makefile 中给出错误,而不是在 bash 中给出错误
Makefile 如下:
THIS.txt : foo.txt
grep THIS foo.txt > $@
当 grep 输出为空时(foo.txt 中没有 THIS),make 会给出错误消息,而 bash 不会:
$ make
make:*** [THIS.txt] Error 1
$ grep THIS foo.txt > THIS.txt
$ grep THIS foo.txt 2>&1
怎么回事?当 grep
输出为空时,我应该如何修改 makefile 以避免出现错误消息?
Makefile is as follows :
THIS.txt : foo.txt
grep THIS foo.txt > $@
When grep output is empty (no THIS in foo.txt), make gives an error message, bash does not :
$ make
make:*** [THIS.txt] Error 1
$ grep THIS foo.txt > THIS.txt
$ grep THIS foo.txt 2>&1
How come? How should I modify my makefile to avoid an error message when grep
output is empty?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
grep
在 bash 中不会给出错误,但它确实返回一个非零退出代码:如果您想摆脱该非零退出代码,那么
make
不会将其标记为错误,您可以这样做:|| true
位表示“如果存在非零退出代码,则返回true
的退出代码(在 bash 中始终为0
)。grep
doesn't give an error in bash, but it does return a non-zero exit code:If you want to get rid of that non-zero exit code, so that
make
won't flag it as an error, you can do this:The
|| true
bit says "if there is a nonzero exit code, return the exit code oftrue
instead (which is always0
in bash).