在此脚本中,将拉取所有 git 存储库:
#!/bin/bash
find / -type d -name .git 2>/dev/null |
while read gitFolder; do
if [[ $gitFolder == *"/Temp/"* ]]; then
continue;
fi
if [[ $gitFolder == *"/Trash/"* ]]; then
continue;
fi
if [[ $gitFolder == *"/opt/"* ]]; then
continue;
fi
parent=$(dirname $gitFolder);
echo "";
echo $parent;
(git -C $parent pull && echo "Got $parent") &
done
wait
echo "Got all"
Run Code Online (Sandbox Code Playgroud)
不wait
等待所有git pull
子 shell。
为什么会这样?我该如何解决?
Kus*_*nda 17
问题是wait
由错误的 shell 进程运行。在 中bash
,管道的每个部分都在单独的子 shell 中运行。后台任务属于执行循环的子 shell while
。将 移动wait
到该子 shell 中将使其按预期工作:
find ... |
{
while ...; do
...
( git -C ... && ... ) &
done
wait
}
echo 'done.'
Run Code Online (Sandbox Code Playgroud)
您还有一些未加引号的变量。
我将完全摆脱管道,而是find
直接运行循环,这样就不需要解析find
.
find / -type d -name .git \
! -path '*/Temp/*' \
! -path '*/opt/*' \
! -path '*/Trash/*' \
-exec sh -c '
for gitpath do
git -C "$gitpath"/.. pull &
done
wait' sh {} +
Run Code Online (Sandbox Code Playgroud)
或者,使用-prune
以避免进入任何我们不想处理的子目录,
find / \( -name Temp -o -name Trash -o -name opt \) -prune -o \
-type d -name .git -exec sh -c '
for gitpath do
git -C "$gitpath"/.. pull &
done
wait' sh {} +
Run Code Online (Sandbox Code Playgroud)
正如评论中提到的,您还可以xargs
更好地控制并发运行的进程的数量git
。下面使用的选项-P
(用于指定并发任务的数量)是非标准的,-0
(用于读取\0
- 分隔的路径名)和-r
(用于避免在没有输入时运行命令)。不过, GNUxargs
和该实用程序的其他一些实现有这些选项。此外,(输出分隔路径名)-print0
的谓词是非标准的,但通常实现。find
\0
find / \( -name Temp -o -name Trash -o -name opt \) -prune -o \
-type d -name .git -print0 |
xargs -t -0r -P 4 -I {} git -C {}/.. pull
Run Code Online (Sandbox Code Playgroud)
我确信 GNUparallel
也可以以类似的方式使用,但由于这不是这个问题的主要焦点,我不追求这个思路。
归档时间: |
|
查看次数: |
659 次 |
最近记录: |