简体   繁体   English

用于选择子目录中的 .c 文件的 bash 脚本

[英]bash script to select .c files in subdirectories

My problem is slightly weird so bear with me.我的问题有点奇怪,所以请耐心等待。

In my current path, I have 3 folders named folder1 , folder2 and folder3.在我当前的路径中,我有 3 个文件夹,分别名为 folder1、folder2 和 folder3。

In each folder is a C program implemented over multiple files and subdirectories.在每个文件夹中都有一个在多个文件和子目录上实现的 C 程序。 For each folder , I want to run the command对于每个文件夹,我想运行命令

sea <file1.c> <file2.c> ... 

where <file1.c> , <file2.c> ... are ALL the .c files present in folder 1 or in some subfolder of folder1 or some subfolder of a subfolder of folder1 and so on...其中 <file1.c> , <file2.c> ... 是文件夹 1 或文件夹 1 的某个子文件夹或文件夹 1 子文件夹的某个子文件夹中存在的所有 .c 文件,依此类推...

I want to write a bash script to do so.我想编写一个 bash 脚本来做到这一点。

I create a FOLDERS variable, with:我创建了一个 FOLDERS 变量,其中包含:

FOLDERS=(folder1 folder2 folder3)

I can iterate over each entry in FOLDERS, but my question is how to collect all the .c files of given folder in a new variable like CFILES and then use that variable for a instruction like:我可以遍历 FOLDERS 中的每个条目,但我的问题是如何将给定文件夹的所有 .c 文件收集到一个新变量(如 CFILES)中,然后将该变量用于如下指令:

sea $(CFILES)

for each folder?每个文件夹?

Don't.别。

for d in folder[123]; do ( cd $d && sea *.c ); done

The only complication I see is if the order of arguments has to be something other than the lexical order this will generate.我看到的唯一复杂问题是,参数的顺序是否必须不是这将生成的词汇顺序。

If you don't absolutely have to be IN the folder for the code to work, then you don't need the subshell parens.如果您不是绝对必须在文件夹中才能使代码工作,那么您不需要子外壳括号。

for d in folder[123]; do sea $d/*.c; done

If you need all the *.c files in all subdirectories, then use globstar .如果您需要所有子目录中的所有*.c文件,请使用globstar

shopt -s globstar
for d in folder[123]; do sea $d/**/*.c; done

This should limit files processed to those ending in .c under one of folder1 folder2 folder3 but including any subdirectories to any depth.这应该将处理的文件限制为在文件folder1 folder2 folder3 1 文件folder1 folder2 folder3.c结尾的文件,但包括任何深度的任何子目录。

Or, as above, if you need to run the script directly in the directory with each file:或者,如上,如果您需要直接在每个文件所在的目录中运行脚本:

shopt -s globstar
for d in folder[123]/**/; do ( cd $d && sea *.c ); done

If your list of directories is not as simple or short as folder1 folder2 folder3 , an array is best.如果您的目录列表不像folder1 folder2 folder3那样简单或简短,则最好使用数组。

dirs=( folder[123] otherFolder /some/full/path another/relative/*/path )

Then use the same logic as needed.然后根据需要使用相同的逻辑。

for d in "${dirs[@]}"; do sea $d/**/*.c; done

or要么

for d in "${dirs[@]}"/**/; do ( cd $d && sea *.c ); done

Just please be sure you understand what each is doing.请确保您了解每个人在做什么。 Feel free to ask questions if you don't.如果您不这样做,请随时提出问题。

Use the globstar option to allow wildcards to expand recursively.使用globstar选项允许通配符递归扩展。

shopt -s globstar
for folder in "${FOLDERS[@]"; do
    sea "$folder"/**/*.c

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

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