Bash多个-execdir或-exec语句在划分为多行以获得正确的代码格式时不起作用



正如我在问题中已经指出的,当find看到某个子目录中存在特定目录时,我必须在该子目录中执行多个命令。

现在,当我在find子句中只有一个-execdir语句时,它的工作原理如下:

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' _ {} ;

目前还不清楚您是如何在每个命令中实际使用目录名的;我只是简单地用"$1"替换了{}的每次使用;不过,这可能不会如你所愿。

相关内容

最新更新