如果不存在,请在makefile中创建一个文件夹 - 错误

发布于 2025-02-12 12:43:44 字数 692 浏览 5 评论 0 原文

我通过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 之前,甚至不应该执行第二个命令。

我该如何解决?

I searched over the internet for a command to create a folder if it doesn't exist. I found it and put in my makefile

.DEFAULT_GOAL := all
folder := "myfolder"

createfolder:
    [ ! -d ${folder} ] && mkdir -p ${folder}

nextstep:
    echo "Got here!"

all: createfolder nextstep

When the folder doesn't exist it's created correctly. But I get an error if the folder already exists.

$ 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

I don't get why the command would give an error if the condition [ ! -d "myfolder" ] is before the mkdir and it shouldn't even execute the second command.

How can I solve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸里的那抹悲凉 2025-02-19 12:43:44

这就是要做的。

${folder}: 
        mkdir -p ${folder}

this is what make is for.

${folder}: 
        mkdir -p ${folder}
内心荒芜 2025-02-19 12:43:44

我认为创建文件夹的最佳方法是使用

%/.:
    mkdir -p "$@"

这样,您可以可靠地为任何规则创建目录:

nextstep: | $(folder)/.
    ...

如果您启用次要扩展规则:

$(OBJDIR)/%.o : $(SRCDIR)/%.c | $(@D)/.
    ...

因此,只需解决 | $$(@d)/。在规则末尾,您都设置了。

I think the best way to create folders is to use order-only prerequisites:

%/.:
    mkdir -p "$@"

With that, you can reliably create directories for any rule:

nextstep: | $(folder)/.
    ...

If you enable secondary expansion, you can use the same trick for arbitrary compilation rules:

$(OBJDIR)/%.o : $(SRCDIR)/%.c | $(@D)/.
    ...

so just tack | $$(@D)/. at the end of the rule and you're all set.

薄暮涼年 2025-02-19 12:43:44

您可以使用 <代码>然后使用 fi 语法

.DEFAULT_GOAL := all
folder := "myfolder"

createfolder:
        if [ ! -d ${folder} ]; then mkdir -p ${folder}; fi

nextstep:
        echo "Got here!"

all: createfolder nextstep

You can use if then fi syntax

.DEFAULT_GOAL := all
folder := "myfolder"

createfolder:
        if [ ! -d ${folder} ]; then mkdir -p ${folder}; fi

nextstep:
        echo "Got here!"

all: createfolder nextstep
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文