您如何使用mktemp在makefile中创建目录?

发布于 2025-01-26 17:09:26 字数 942 浏览 1 评论 0原文

这是一个制造费的代码,说明某人如何在makefile中使用mktemp

TEST=$(shell mktemp -d)
mktemp:
    echo $(TEST)
    touch $(TEST)/test.txt
    ls $(TEST)
    cat $(TEST)/test.txt
    rm -rf $(TEST)

这是一个示例,输出的示例

❯ make mktemp 
echo /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.tQI5EeyW
/var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.tQI5EeyW
touch /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.lVL3N8Rp/test.txt
ls /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.sBW9FzgD
cat /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.Ti53SWSw/test.txt
cat: /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.Ti53SWSw/test.txt: No such file or directory
make: *** [mktemp] Error 1

是期望cat/var/folders/62/wkysd_4n0w57ljl9ycfsd9ycfsd9cc0000 gn/tmp.tmp.tmp.tmp.ti53swswswst.test.test.test.test.test.test.test.test.teast.teast.test. >不会出错。

在这种情况下,如何使用MKTEMP?

This is a Makefile piece of code of how someone may use mktemp in a Makefile

TEST=$(shell mktemp -d)
mktemp:
    echo $(TEST)
    touch $(TEST)/test.txt
    ls $(TEST)
    cat $(TEST)/test.txt
    rm -rf $(TEST)

This is an example output

❯ make mktemp 
echo /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.tQI5EeyW
/var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.tQI5EeyW
touch /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.lVL3N8Rp/test.txt
ls /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.sBW9FzgD
cat /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.Ti53SWSw/test.txt
cat: /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.Ti53SWSw/test.txt: No such file or directory
make: *** [mktemp] Error 1

The expectation is that cat /var/folders/62/wkysd_4n0w57ljl9ycfsd9cc0000gn/T/tmp.Ti53SWSw/test.txt would not error.

How can mktemp be used in this case?

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

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

发布评论

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

评论(1

黄昏下泛黄的笔记 2025-02-02 17:09:26

此行:

TEST=$(shell mktemp -d)

test变量的值设置为字符串$(shell mktemp -d)。它不会扩展该字符串(不运行shell命令),而只能将字符串保持在原样。

现在,每当您以后在makefile中使用该值时,它会进行扩展,这意味着mktemp再次运行,并且您获得了不同的值:

mktemp:
        echo $(TEST)
        touch $(TEST)/test.txt
        ls $(TEST)
        cat $(TEST)/test.txt
        rm -rf $(TEST)

您想使用

TEST := $(shell mktemp -d)

另外,您只需使用shell操作编写配方,而不使用任何制造功能,例如shell

mktemp:
        TEST=$(mktemp -d) && \
        echo $TEST && \
        touch $TEST/test.txt && \
        ls $TEST && \
        cat $TEST/test.txt && \
        rm -rf $TEST

默认情况下,请注意配方的每条逻辑行都在单独的外壳中运行,因此为了拥有全部这些行在同一外壳中运行(因此它们可以访问相同的$ test变量),您需要使用BackSlash将它们组合到单个逻辑线中。

This line:

TEST=$(shell mktemp -d)

sets the value of the TEST variable to the string $(shell mktemp -d). It doesn't expand that string (doesn't run the shell command), it just keeps the string as-is.

Now, every time you use that value later in the makefile, it's expanded which means mktemp is run again, and you get a different value:

mktemp:
        echo $(TEST)
        touch $(TEST)/test.txt
        ls $(TEST)
        cat $(TEST)/test.txt
        rm -rf $(TEST)

You want to use immediate expansion when you assign the variable, so that it is expanded only one time when the makefile is parsed; use:

TEST := $(shell mktemp -d)

Alternatively you can just write the recipe using shell operations and not use any make functions like shell:

mktemp:
        TEST=$(mktemp -d) && \
        echo $TEST && \
        touch $TEST/test.txt && \
        ls $TEST && \
        cat $TEST/test.txt && \
        rm -rf $TEST

Note by default each logical line of a recipe is run in a separate shell, so in order to have all the lines run in the same shell (so they have access to the same $TEST variable) you need to use backslash to combine them into a single logical line.

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