简体   繁体   English

如何将Shell命令的结果存储在Makefile中?

[英]How do I store the results of a shell command in a makefile?

Locally I'm usin GNU Make 3.8 on MacOS (i386-apple-darwin11.3.0) 我在本地使用MacOS上的GNU Make 3.8(i386-apple-darwin11.3.0)

I'm trying to generate a bunch of test files based on the files in a directory. 我正在尝试根据目录中的文件生成一堆测试文件。

Right now I'm able to grab the basename of the files, but I cant figure out how to store them in a variable so I can generate another file with the basename 现在,我可以获取文件的基本名称,但是我无法弄清楚如何将它们存储在变量中,因此可以生成另一个具有基本名称的文件

TEST_FILES = lib/*.js

build-test:
    @mkdir -p tests
    -@for file in $(TEST_FILES); do \
        echo $$file; \
        MYNAMES = $(basename $$file .js) \
        echo $MYNAMES; \
    done

I want to then create a bunch of files called $MYNAME.log using output from a STDOUT stream. 然后,我想使用STDOUT流的输出创建一堆名为$ MYNAME.log的文件。

I'm stuck on getting the names into the variable, the solutions I've found so far are throwing errors for me when I try to implement them 我一直坚持将名称放入变量中, 到目前为止 ,当我尝试实现它们时,我发现的解决方案正在为我抛出错误

This line here 这条线在这里

echo $MYNAMES; \

will substitute in the value of the make variable M , as if you wrote $(M)YNAMES. 将替换make变量M的值,就像您写了$(M)YNAMES一样。 You want 你要

echo $$MYNAMES; \

instead. 代替。

This line 这条线

   MYNAMES = $(basename $$file .js) \

cannot work - you are using a make command basename which is executed only once before the subshell is invoked to execute the rule, whereas file is a shell variable which changes each iteration of the loop. 不能正常工作-您正在使用一个make命令的basename ,该basename只能在调用子shell执行规则之前执行一次,而file是一个shell变量,它会更改循环的每次迭代。 You probably meant $$(basename $$file) instead, which will be passed to the shell as "$(basename $file)". 您可能是用$$(basename $$file)代替的,它将作为“ $(basename $ file)”传递到外壳。 The shell will insert the results of running the basename command. Shell将插入运行basename命令的结果。

Make sure that you do want to store them in a shell variable, and not as a make variable. 确保确实要将它们存储在shell变量中,而不是作为make变量。 It's essential to understand the difference. 了解差异至关重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM