如果不存在,请在makefile中创建一个文件夹 - 错误
我通过Internet搜索了一个命令,以创建文件夹,如果它不存在。我找到了它并放入我的makefile
.DEFAULT_GOAL := all
folder := "myfolder"
createfolder:
[ ! -d ${folder} ] && mkdir -p ${folder}
nextstep:
echo "Got here!"
all: createfolder nextstep
时,当该文件夹不存在时,它是正确创建的。但是如果文件夹已经存在,我会遇到错误。
$ ls
makefile
$ make
[ ! -d "myfolder" ] && mkdir -p "myfolder"
echo "Got here!"
Got here!
$ ls
makefile myfolder
$ make
[ ! -d "myfolder" ] && mkdir -p "myfolder"
make: *** [makefile:5: createfolder] Error 1
我不明白,如果条件 [! -d“ myFolder”]
是在 mkdir
之前,甚至不应该执行第二个命令。
我该如何解决?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是要做的。
this is what make is for.
我认为创建文件夹的最佳方法是使用:
这样,您可以可靠地为任何规则创建目录:
如果您启用次要扩展规则:
因此,只需解决
| $$(@d)/。
在规则末尾,您都设置了。I think the best way to create folders is to use order-only prerequisites:
With that, you can reliably create directories for any rule:
If you enable secondary expansion, you can use the same trick for arbitrary compilation rules:
so just tack
| $$(@D)/.
at the end of the rule and you're all set.您可以使用 <代码>然后使用
fi
语法You can use
if
then
fi
syntax