在Linux中编写一个脚本,在每个子目录maxdepth为2中运行make

时间:2021-02-07 02:21:49

I am looking to write a script to go into each child directory (not recursively) and run a make file to create a binary. One way to do this is to change directories and run make for each folder but it's not very elegant and can be error prone if additional folders and files are added.

我期待编写一个脚本进入每个子目录(不递归)并运行make文件来创建二进制文件。一种方法是更改​​目录并为每个文件夹运行make,但它不是很优雅,如果添加其他文件夹和文件,则可能容易出错。

I have had a little success delving into child dirs with the following:

我通过以下方式深入研究了儿童用品:

find -maxdepth 2 -type f -print -exec make {} . \;

find -maxdepth 2-type f -print -exec make {}。 \;

I get the error for each dir which states the following:

我得到每个目录的错误,说明以下内容:

make: Nothing to be done for reduc211/makefile.

make:reduc211 / makefile没什么可做的。

Anyone got any ideas as to what I can change? Any help would be greatly appreciated!

任何人都对我能改变什么有任何想法?任何帮助将不胜感激!

Many thanks and happy coding.

非常感谢和愉快的编码。

2 个解决方案

#1


1  

make reduc211/makefile doesn't run the named makefile; it looks for a Makefile using make's lookup rules and tries to make the target reduc211/makefile. What you want is something like

make reduc211 / makefile不运行命名的makefile;它使用make的查找规则查找Makefile,并尝试生成目标reduc211 / makefile。你想要的是什么

find -maxdepth 2 -name 'Makefile' -print -execdir make \;

This runs the make command in every directory where a file named Makefile is found.

这会在找到名为Makefile的文件的每个目录中运行make命令。

If you have differently named makefiles, for example each is of the form Makefile.something, you could try

如果您有不同名称的makefile,例如每个都是Makefile.something形式,您可以尝试

find -maxdepth 2 -name 'Makefile.*' -print -execdir make -f \{}\ \;

to run make using the specific Makefiles found by find.

使用find找到的特定Makefile运行make。

#2


0  

Something like this?

像这样的东西?

for dir in *; do
    if [ -d "$dir" ]; then
        (cd "$dir" && make)
    fi
end

#1


1  

make reduc211/makefile doesn't run the named makefile; it looks for a Makefile using make's lookup rules and tries to make the target reduc211/makefile. What you want is something like

make reduc211 / makefile不运行命名的makefile;它使用make的查找规则查找Makefile,并尝试生成目标reduc211 / makefile。你想要的是什么

find -maxdepth 2 -name 'Makefile' -print -execdir make \;

This runs the make command in every directory where a file named Makefile is found.

这会在找到名为Makefile的文件的每个目录中运行make命令。

If you have differently named makefiles, for example each is of the form Makefile.something, you could try

如果您有不同名称的makefile,例如每个都是Makefile.something形式,您可以尝试

find -maxdepth 2 -name 'Makefile.*' -print -execdir make -f \{}\ \;

to run make using the specific Makefiles found by find.

使用find找到的特定Makefile运行make。

#2


0  

Something like this?

像这样的东西?

for dir in *; do
    if [ -d "$dir" ]; then
        (cd "$dir" && make)
    fi
end