Bash 递归读取文件夹,如果文件夹中存在文件,则对每个文件夹执行命令

Bash 递归读取文件夹,如果文件夹中存在文件,则对每个文件夹执行命令,bash,shell,Bash,Shell,我有这个命令,它功能齐全,非常喜欢 for d in ./*/ ; do (cd "$d" && ../Process.sh); done 它遍历所有子文件夹并在每个文件夹中运行Process.sh 我需要的是,如果文件夹中存在“kill_by_pid”文件,它只需运行进程.sh。除非该特定文件夹中不存在“kill_by_pid”,否则将跳过该文件夹并将其移动到下一个文件夹。尝试此操作 for d in ./*/ ; do if [ -f "$d"/kill_by_p

我有这个命令,它功能齐全,非常喜欢

for d in ./*/ ; do (cd "$d" && ../Process.sh); done
它遍历所有子文件夹并在每个文件夹中运行Process.sh

我需要的是,如果文件夹中存在“kill_by_pid”文件,它只需运行
进程.sh
。除非该特定文件夹中不存在“kill_by_pid”,否则将跳过该文件夹并将其移动到下一个文件夹。

尝试此操作

for d in ./*/ ;
  do 
  if [ -f "$d"/kill_by_pid ]; then 
    (cd "$d" && ../Process.sh);
  fi
done

稍微不同的方法:

for f in ./*/kill_by_pid; do
    pushd -- "${f#/*}"
    ../Process.sh
    popd
done
这将按pid查找每个
kill\u文件,然后确定包含该文件的目录的名称。(使用
pushd
/
popd
只是为了演示
在当前shell会话中执行
Process.sh
时,一种更改和恢复不需要子shell的当前目录的方法。)

如何使用
find
execdir
,例如:

find . -type f -name "kill_by_pid" -execdir ../Process.sh \;

这将只调用父目录中存在文件
kill\u by\u pid
的脚本
Process.sh

我遇到了这个错误:第2行的语法错误:`if'出乎意料