简体   繁体   中英

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)

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.

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. 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. You probably meant $$(basename $$file) instead, which will be passed to the shell as "$(basename $file)". The shell will insert the results of running the basename command.

Make sure that you do want to store them in a shell variable, and not as a make variable. It's essential to understand the difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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