`2>/dev/null`在Makefile中不起作用 [英] `2>/dev/null` does not work inside a Makefile

查看:219
本文介绍了`2>/dev/null`在Makefile中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过写抑制来自rm命令的错误

I tried to suppress an error from rm command by writing

Makefile:

...
clean: $(wildcard *.mod)
    -rm $^ 2>/dev/null
...

我跑了

$ make clean
rm  2>/dev/null
make: [clean] Error 64 (ignored)

我还是出错了.

无论如何,当我尝试

$ rm [some non-existent files] 2>/dev/null

在bash外壳上,它工作正常.

on the bash shell, it just works fine.

如何在Makefile中使用2>/dev/null?

How can I use 2>/dev/null inside a makefile?

推荐答案

make 显示消息make: [clean] Error 64 (ignored)后,发现您的shell命令失败. 因此,它将不受您在配方中使用的任何重定向的影响.

The message make: [clean] Error 64 (ignored) is being printed by make after it sees that your shell command has failed. It will therefore not be affected by any redirection that you use in the recipe.

两个修复程序:

  1. 使用-f rm 标志. rm -f从不返回错误. (好吧,反正几乎没有,如果可能的话,您可能想知道!)

  1. Use the -f rm flag. rm -f never returns an error. (Well, hardly ever anyway, and if it does you probably want to know about it!)

停止返回错误的shell命令:只需将|| :附加到命令即可. 说什么?好吧,如果rm成功,那么您的工作就完成了,而 make 很高兴. OTOH如果rm失败,则外壳程序将在中运行第二条命令. :是始终成功的内置Shell,比true恕我直言更可取.

Stop the shell command returning an error: simply append || : to the command. Say what? Well if the rm succeeds your job is done and make is happy. OTOH if rm fails, the shell runs the second command in the or. : is a shell built-in that always succeeds, and is much preferable to true IMHO.

在这种情况下,第一个最好 尽管第二种是通用的模式,尽管效率略低.

The first of these is best in this case, though the second is a general, if somewhat less efficient, pattern.

.PHONY: clean
clean: ; rm -rf *.mod

这篇关于`2>/dev/null`在Makefile中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆