Mar*_*ark 1 linux bash shell automation loops
我有一段代码可以运行目录中的所有脚本: 运行文件夹中的所有shell脚本
for f in *.sh; do \
bash "$f" -H || break
done
Run Code Online (Sandbox Code Playgroud)
我也有运行一系列.sh脚本的代码:
for f in {1..3}madeupname.sh; do \
bash "$f" -H || break
done
Run Code Online (Sandbox Code Playgroud)
现在,我不想运行所有.sh脚本或一系列.sh脚本,而是要运行“ N”个.sh脚本,其中N是任意数量,例如3个.sh脚本。
对我来说,N个文件的运行顺序并不重要。
find
脚本,获取head
,然后使用执行xargs
。
find . -name '*.sh' | head -n 10 | xargs -n1 sh
Run Code Online (Sandbox Code Playgroud)
您可以xargs
使用一个简单的-P0
选项并行运行脚本。您可以编写xargs
带有某些脚本的脚本xargs sh -c 'bash "$@" -H || exit 125' --
,使脚本xargs
以非零状态退出,或者在任何脚本无法运行等情况后立即退出。
如果您不熟悉xargs
,请做一个简单的while read
循环:
find . -name '*.sh' | head -n 10 |
while IFS= read -r script; do
bash "$script" -H || break
done
Run Code Online (Sandbox Code Playgroud)
同时,您必须退出管道子外壳:
while IFS= read -r script; do
bash "$script" -H || break &
done < <(
find . -name '*.sh' | head -n 10
)
wait # for all the childs
Run Code Online (Sandbox Code Playgroud)
或在子外壳本身中等待孩子:
find . -name '*.sh' | head -n 10 |
{
while IFS= read -r script; do
bash "$script" -H || break &
done
wait
}
Run Code Online (Sandbox Code Playgroud)