我是 Linux 的新手,也是编程的初学者,但是已经能够在这里修补几行代码, 将文件列表传递给 for 循环,在这里对文件夹执行相同的操作。
# Run search.sh from base folder;
# Only interested in folders under base folder (e.g., baseFolder/FolderA)
# list all folders in base directory that match search parameters;
# cut out just the folder name; feed to variable DIR
DIR=$(find . -name *MTL.txt | cut -d '/' -f2)
# echo $DIR = "FolderA FolderB FolderC"
# place that information in a for-loop
for i in $DIR; do
cd $DIR # step into folder
# find specific file in folder for processing
FILE=$(find -name *MTL | cut -d '/' -f2)
# copy in a static file from another folder;
# rename file based on file name found in previous step
cp /baseFolder/staticfile.txt $FILE.new
do more stuff
cd .. # step out of directory
done
该代码在第一个目录中完成得很好,但无法进入后续目录。我猜我的(许多)问题之一是我不能像我一样将文件夹名称列表传递给 $DIR 。这应该很简单,但我的 foo 很弱。
请老师给我指路。
编辑:
将“cd $DIR”更改为“cd $i”具有预期的效果。代码现在循环遍历所有目录并在每个目录中正确执行操作。- 感谢 core1024 用于标记上述内容。