如何检查 Makefile 中是否存在文件以便将其删除?
在我的 Makefile
的干净部分中,我尝试在永久删除之前检查该文件是否存在。我使用此代码但收到错误。
有什么问题吗?
if [ -a myApp ]
then
rm myApp
fi
我收到此错误消息
if [ -a myApp ]
/bin/sh: Syntax error: end of file unexpected (expecting "then")
make: *** [clean] Error 2
In the clean section of my Makefile
I am trying to check if the file exists before deleting permanently. I use this code but I receive errors.
What's wrong with it?
if [ -a myApp ]
then
rm myApp
fi
I get this error message
if [ -a myApp ]
/bin/sh: Syntax error: end of file unexpected (expecting "then")
make: *** [clean] Error 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
与问题略有不同,但如果您有一个包含要删除的文件列表的变量,您可以执行以下操作:
基本上,您循环遍历目标变量定义的文件名,并使用“test”检查目标是否存在。如果是,则删除该文件,如果不是,则报告该文件不存在。最后一次检查(报告它不存在)是必要的,因为否则如果根本没有目标,则会引发错误
Slightly different from the question, but in case you have a variable containing a list of files which you want to delete you can do
The basically you loop over the filenames defined by the targets variable and check with 'test' if the target exists. If yes, delete the file, if not, report it is not there. The last check (reporting it is not there) is necessary because otherwise an error is raised in case there is no target at all
我想使用上面的命令,但是信誉:)
你可以通过添加 .ONESHELL: 指令在 gnu make 目标中使用多行命令:
这消除了尝试想出创造性的单行或反斜杠的一切。
I wanted to command above, but reputation :)
You can have multi-line commands in gnu make targets by adding the .ONESHELL: directive:
This eliminates trying to come up with creative one-liners or backslash everything.
我们还可以利用 GNU Coreutils 的
test
内置函数 (手动),这意味着我们可以像这样检查文件是否存在:或者更简洁,像这样:
We can also take advantages from GNU Coreutils's
test
builtin (manual), which means we can check file existence like this:or more neatly, like this:
看到这么多人为此使用 shell 脚本,这很奇怪。我一直在寻找一种使用本机 makefile 语法的方法,因为我是在任何目标之外编写此语法的。您可以使用
通配符
函数检查文件是否存在:另请参阅:
更新:
我找到了一种我真正喜欢的方法:
It's strange to see so many people using shell scripting for this. I was looking for a way to use native makefile syntax, because I'm writing this outside of any target. You can use the
wildcard
function to check if file exists:See also:
Update:
I found a way which I really like:
第二个最佳答案提到了 ifeq,但是,它没有提到这个 ifeq 在 makefile 中必须与目标名称具有相同的缩进级别,例如仅当当前不存在时才下载文件,可以使用以下代码:
The second top answer mentions
ifeq
, however, it fails to mention that thisifeq
must be at the same indentation level in the makefile as the name of the target, e.g., to download a file only if it doesn't currently exist, the following code could be used:问题是当您将命令拆分为多行时。因此,您可以像上面一样在行尾使用
\
来继续,也可以在 bash 中使用&&
运算符将所有内容放在一行上。然后你可以使用
test
命令来测试文件是否存在,例如:或不:
test
相当于[
命令。它会像你原来的例子一样工作。
请参阅:
help [
或help test
了解更多语法。The problem is when you split your command over multiple lines. So, you can either use the
\
at the end of lines for continuation as above or you can get everything on one line with the&&
operator in bash.Then you can use a
test
command to test if the file does exist, e.g.:or does not:
The
test
is equivalent to[
command.and it would work as in your original example.
See:
help [
orhelp test
for further syntax.它可能需要在行尾有一个反斜杠才能继续(尽管这可能取决于 make 的版本):
It may need a backslash on the end of the line for continuation (although perhaps that depends on the version of make):
或者只是将它放在一行上,就像
make
喜欢的那样:Or just put it on one line, as
make
likes it:一行解决方案:
带有错误操作的一行解决方案:
我的
make clean
指令中使用的示例:并且
make clean
工作时没有错误消息!One line solution:
One line solution with error action:
Example used in my
make clean
directives:And
make clean
works without error messages!执行结果:
execution results:
缺少分号
但是,我假设您在删除之前检查是否存在,以防止出现错误消息。如果是这样,您可以使用 rm -f myApp 来“强制”删除,即如果文件不存在也不会出错。
Missing a semicolon
However, I assume you are checking for existence before deletion to prevent an error message. If so, you can just use
rm -f myApp
which "forces" delete, i.e. doesn't error out if the file didn't exist.上面发布的这个解决方案效果最好。但请确保您没有将 PATH_TO_FILE 分配字符串化
例如,
它必须是
This solution posted above works best. But make sure that you do not stringify the PATH_TO_FILE assignment
E.g.,
It must be
使用
test
命令检查文件是否存在,然后使用rm
删除它。\文件命令的语法是 -
删除文件的语法是 -
所以现在我们需要一个命令来删除文件(仅当文件存在时),因此我们仅将 OR
||
与测试命令一起使用,可以通过使用和
&&
来使用多个命令括号内()
Use
test
command to check if the file exists or not and then userm
to delete it.\Syntax for the file command is -
Syntax for deleting the file is -
So now we need a command to delete the file only if it exists so we will only use OR
||
with the test commanduse can use multiple commands by using and
&&
within the parenthesis()
答案类似于 @mark-wilkins 的答案,使用 \ 来继续行,并且 ;在 shell 中终止它们或者像 @kenorb 中的那样将其更改为一行都很好,并且可以解决此问题。
对于原始问题有一个更简单的答案(正如@alexey-polonsky 指出的那样)。使用 rm 的 -f 标志,这样就不会触发错误,
这样更简单、更快、更可靠。只是要小心,不要以斜杠和空变量
rm -f /$(myAppPath)#NEVER DO THIS你可能最终会删除你的系统。
The answers like the one from @mark-wilkins using \ to continue lines and ; to terminate them in the shell or like the ones from @kenorb changing this to one line are good and will fix this problem.
there's a simpler answer to the original problem (as @alexey-polonsky pointed out). Use the -f flag to rm so that it won't trigger an error
this is simpler, faster and more reliable. Just be careful not to end up with a slash and an empty variable
rm -f /$(myAppPath)#NEVER DO THISyou might end up deleting your system.
“如果 README.md 不存在,则不执行任何操作(并成功退出)。否则,将文本附加到末尾。”
如果您使用
&&
而不是||
那么当文件不存在时会生成错误:"If README.md does not exist, do nothing (and exit successfully). Otherwise, append text to the end."
If you use
&&
instead of||
then you generate an error when the file doesn't exist:我正在尝试:
并且积极的情况有效,但我的 ubuntu bash shell 调用此 TRUE 并在副本上中断:
收到此错误后,我谷歌如何检查 make 中是否存在文件,这就是答案...
I was trying:
And the positive case worked but my ubuntu bash shell calls this TRUE and breaks on the copy:
After getting this error, I google how to check if a file exists in make, and this is the answer...