bash - Bash Multiple -execdir or -exec statements NOT WORKING 当分成多行以进行正确的代码格式化时
问题描述
find
正如我在问题中已经指出的那样,当看到该子目录中存在特定目录时,我必须在该子目录中执行多个命令。
现在,当我在子句中只有一个-execdir
语句时,find
它可以正常工作,如下所示:
find $repoFolder -type d -name '*.git' \
-execdir git config --global credential.helper manager{}/.git \;
但是当我有多个-execdir
语句并且如果我将它们分成多行以使代码看起来不错时,它不起作用,这根本没有意义,它应该起作用:
find $repoFolder -type d -name '*.git' \
-execdir git config --global credential.helper manager{}/.git \; \
-execdir curr_branch=$(git rev-parse --abbrev-ref HEAD){}/.git \; \
-execdir git checkout master && git remote prune origin{}/.git \; \
-execdir git pull && git pull origin{}/.git \; \
-execdir git checkout $curr_branch && git merge master{}/.git \;
谁能帮助找出为什么会出现以下错误?
fatal: not a git repository (or any of the parent directories): .git
find: missing argument to `-execdir'
fatal: not a git repository (or any of the parent directories): .git
find: missing argument to `-execdir'
....
解决方案
有两个问题。首先,每个都-execdir
在一个新的 shell 进程中执行它的命令;curr_branch
没有在它自己的外壳之外定义。其次,&&
终止find
命令(过早地,因为没有\;
找到)。
您需要将所有内容组合到一个复合 shell 命令中,以便与单个-execdir
主节点一起使用。
find "$repoFolder" -type d -name '*.git' \
-execdir sh -c '
git config --global credential.helper manager"$1"/.git;
curr_branch=$(git rev-parse --abbrev-ref HEAD)"$1"/.git;
git checkout master && git remote prune origin"$1"/.git;
git pull && git pull origin"$1"/.git;
git checkout $curr_branch && git merge master"$1"/.git' _ {} \;
目前尚不清楚您实际上是如何在每个命令中使用目录名称的;我只是简单地替换了发现的每次使用{}
with "$1"
;但是,这可能无法按您的意愿工作。