bash 简单循环某个目录makefile中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54020765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
simple loop over files in some directory makefile
提问by OrenIshShalom
I can easily print all the files inside some directory from bash
:
我可以轻松地从以下位置打印某个目录中的所有文件bash
:
$ cat go.sh
BASEDIR=~/Downloads
MYDIR=${BASEDIR}/ddd
for f in $(ls ${MYDIR}); do echo $f; done
$ ./go.sh
m.txt
d.txt
When I try to do a similar thing from makefile
it doesn't work well:
当我尝试从中做类似的事情时,makefile
效果不佳:
$ cat makefile
BASEDIR = ${HOME}/Downloads
MYDIR = ${BASEDIR}/ddd
all:
for f in $(ls ${MYDIR}); do echo ${f}; done
$ make
for f in ; do echo ; done
And here is another trial that doesn't work:
这是另一个不起作用的试验:
$ cat makefile
BASEDIR = ${HOME}/Downloads
MYDIR = ${BASEDIR}/ddd
all:
for f in $(shell ls ${MYDIR}); do echo ${f}; done
$ make
for f in d.txt m.txt; do echo ; done
采纳答案by Oo.oO
Maybe you can do it purely Makefile way?
也许你可以用纯粹的 Makefile 方式做到这一点?
MYDIR = .
list: $(MYDIR)/*
@echo $^
You can still run command from Makefile like this
您仍然可以像这样从 Makefile 运行命令
MYDIR = .
list: $(MYDIR)/*
for file in $^ ; do \
echo "Hello" $${file} ; \
done
If I were you, I'd rather not mix Makefile and bash loops based on $(shell ...)
. I'd rather pass dir name to some script and run loop there - inside script.
如果我是你,我宁愿不混合基于$(shell ...)
. 我宁愿将目录名称传递给某个脚本并在那里运行循环 - 脚本内部。
回答by OrenIshShalom
Here is the edited answer based on @Oo.oO:
这是基于@Oo.oO 的编辑答案:
$ cat makefile
BASEDIR = ${HOME}/Downloads
MYDIR = ${BASEDIR}/ddd
all:
@for f in $(shell ls ${MYDIR}); do echo $${f}; done
$ make
d.txt
m.txt
回答by Mike Pylypyshyn
Also almost "true way" from documentation
从文档中也几乎是“真实的方式”
TEMPLATES_DIR = ./somedir
list:
$(foreach file, $(wildcard $(TEMPLATES_DIR)/*), echo $(file);)
回答by Zhwt
There is a little problem with @Oo.oO's answer.
@Oo.oO 的回答有点问题。
If there is any file/folder has the same name with a targetin makefile, and that target has some prerequisites, and you want to loop through that folder, you will get that target recipe being executed.
如果有任何文件/文件夹与makefile 中的目标同名,并且该目标具有一些先决条件,并且您想遍历该文件夹,则会执行该目标配方。
For example: if you have a folder named build
, and you have a rule like:
例如:如果您有一个名为 的文件夹build
,并且您有如下规则:
build: clean server client
clean:
@echo project cleaned!
server:
@echo server built!
client:
@echo client built!
To loop through the folder contains that special build
folder, let's says you have the following rules:
要遍历包含该特殊build
文件夹的文件夹,假设您有以下规则:
MYDIR = .
ls: $(MYDIR)/*
@echo $^
The result will be:
结果将是:
$ make ls
project cleaned!
server built!
client built!
build Makefile
I would suggest to use @Mike Pylypyshyn's solution. According to the make documentation, the foreach function is more suitable in this case.
我建议使用@Mike Pylypyshyn 的解决方案。根据make 文档,foreach 函数更适合这种情况。