我想编写一个bash脚本(递归地)处理某种类型的所有文件.
我知道我可以通过使用find得到匹配的文件列表:
找 .-name"*.ext"
我想在脚本中使用它:
我的第一次尝试看起来像(伪代码):
ROOT_DIR = ~/work/projects
cd $ROOT_DIR
for f in `find . -name "*.ext"`
do
#need to lop off leading './' from filename, but I havent worked out how to use
#cut yet
newname = `echo $f | cut -c 3
filename = "$ROOT_DIR/$newname"
retcode = ./some_other_script $filename
if $retcode ne 0
logError("Failed to process file: $filename")
done
Run Code Online (Sandbox Code Playgroud)
这是我第一次尝试编写bash脚本,因此上面的代码段不太可能运行.但是,希望我正在尝试做的事情的逻辑足够清楚,有人可以展示如何加入点并将上面的伪代码转换为工作脚本.
我在Ubuntu上运行
Ign*_*ams 12
find . -name '*.ext' \( -exec ./some_other_script "$PWD"/{} \; -o -print \)
Run Code Online (Sandbox Code Playgroud)