如何忽略 mv 错误?
我正在制作一个 Makefile,将输出文件 (foo.o
) 移动到不同的目录 (baz
)。
输出文件根据需要移动到目录。但是,由于如果我再次输入 make
,make 不会重新编译输出文件,因此 mv
在尝试将不存在的空文件移动到目录 时会收到错误>巴兹。
这就是我在所有编译后在规则 make all
中定义的内容:
-test -e "foo.o" || mv -f foo.o ../baz
不幸的是,我仍然收到错误。
I'm making a Makefile that moves an output file (foo.o
) to a different directory (baz
).
The output file moves as desired to the directory. However since make won't recompile the output file if I type make
again, mv
gets an error when it tries to move the non-existent empty file to the directory baz
.
So this is what I have defined in my rule make all
after all compilation:
-test -e "foo.o" || mv -f foo.o ../baz
Unfortunately, I'm still getting errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
食谱中的错误(来自 TFM )
所以目标是这样的:
Errors in Recipes (from TFM)
So the target would be something like:
我注意到还没有人真正回答原始问题本身,特别是如何忽略错误(所有答案目前都只涉及在不会导致错误的情况下调用命令)。
要真正忽略错误,您可以简单地执行以下操作:
这会将
stderr
输出重定向到null
,并按照true
执行命令(始终返回 < code>0,使 make 相信命令成功,无论实际发生了什么),允许程序流程继续。I notice nobody has actually answered the original question itself yet, specifically how to ignore errors (all the answers are currently concerned with only calling the command if it won't cause an error).
To actually ignore errors, you can simply do:
This will redirect
stderr
output tonull
, and follow the command withtrue
(which always returns0
, causing make to believe the command succeeded regardless of what actually happened), allowing program flow to continue.是我用来静默创建文件夹(如果它不存在)的方法。对于你的问题,这样的事情应该有效
is what I use to silently create a folder if it does not exist. For your problem something like this should work
那应该有效
That should work
类似的东西
应该有效:运算符应该是
&&
而不是||
。您可以通过尝试以下命令来进行实验:
Something like
should work: the operator should be
&&
instead of||
.You can experiment with this by trying these commands:
我遇到了同样的问题,当我生成文件时,它们总是有不同的时间。解决方法是为文件设置相同的时间:touch -d '1 June 2018 11:02' file。在这种情况下,gzip 会生成相同的输出和相同的 md5sum。在我的场景中,我不需要时间来处理文件。
I faced the same problem and as I am generating files, they always have different time. Workaround is set the same time to the files: touch -d '1 June 2018 11:02' file. In that case, gzip generates the same output and same md5sum. In my scenario, I don't need the time for the files.