如何使用子文件夹中的文件名填充数组以在嵌套循环中使用?

How can I populate an Array to use in a nested loop with filenames from a subfolder?

我不怎么用bash所以对我来说很简单!

我有一个 while 循环,它在每个文件夹中搜索某个文件名 (myFile.yaml),然后执行大量操作。其中一部分还需要涉及子文件夹中的文件(数量不定)。我目前将其设置为循环数组的 for 循环:

files=(thisFile.yaml thatfile.yaml otherfile.yaml)
    for file in files do;
        echo "$folder"/"morefiles"/"$value"
    done


>repo/folder1/morefiles/thisFile.yaml
>repo/folder1/morefiles/thatFile.yaml
>repo/folder1/morefiles/otherFile.yaml
>repo/folder1/morefiles/foo.yaml
> etc.

这按预期工作,但现在我需要一种方法来使用子文件夹中的实际文件填充 files

myFile.yaml 所在的主文件夹具有 PATH $folder,因此文件在 "$folder"/"moreFiles" 中。我很高兴 files 包含完整路径和名称或仅包含文件名。

结构:

Repo > Folder1 > myFile.yaml 
Repo > Folder1 > moreFiles > thisFile.yaml 
Repo > Folder1 > moreFiles > thatfile.yaml 
Repo > Folder1 > moreFiles > otherfile.yaml

Repo > Folder2 > myFile.yaml 
Repo > Folder2 > moreFiles > foo.yaml
Repo > Folder2 > moreFiles > barr.yaml 
Repo > Folder2 > moreFiles > foobar.yaml

只需使用 glob。

shopt -s nullglob  # just to protect
files=("$folder"/morefiles/*)

但如果存储文件没有意义,那么最好不要存储文件。只需遍历它们即可。

for i in "$folder"/morefiles/* ; do

并且您可以获得相同的以换行符分隔的文件输出:

printf "%s\n" "$folder"/morefiles/*