[英]Run “N” Shell Scripts in Folder
I have a working piece of code to run all scripts in a directory: Run all shell scripts in folder 我有一段代码可以运行目录中的所有脚本: 运行文件夹中的所有shell脚本
for f in *.sh; do \
bash "$f" -H || break
done
I also have code to run a sequence of .sh scripts: 我也有运行一系列.sh脚本的代码:
for f in {1..3}madeupname.sh; do \
bash "$f" -H || break
done
Now instead of running all .sh scripts or a range of .sh scripts, I want to run "N" number of .sh scripts where N is an arbitrary number say 3 .sh scripts for example. 现在,我不想运行所有.sh脚本或一系列.sh脚本,而是要运行“ N”个.sh脚本,其中N是任意数量,例如3个.sh脚本。
The order in which N files are run is not important to me. 对我来说,N个文件的运行顺序并不重要。
find
the scripts, get the head
, then execute with xargs
. find
脚本,拿到head
,然后执行xargs
。
find . -name '*.sh' | head -n 10 | xargs -n1 sh
You can run the scripts in parallel with xargs
with a simple -P0
option. 您可以使用简单的
-P0
选项将脚本与xargs
并行运行。 You can script the xargs
with some xargs sh -c 'bash "$@" -H || exit 125' --
您可以使用一些
xargs sh -c 'bash "$@" -H || exit 125' --
为xargs
编写脚本。 xargs sh -c 'bash "$@" -H || exit 125' --
to make xargs
exit with nonzero status or immediately after any of the scripts fail to run or something. xargs sh -c 'bash "$@" -H || exit 125' --
xargs
使xargs
以非零状态退出,或者在任何脚本运行失败或出现某种情况后立即退出。
If you feel like unfamiliar with xargs
, just do a simple while read
loop: 如果您不熟悉
xargs
,只需while read
循环时执行一个简单的操作即可:
find . -name '*.sh' | head -n 10 |
while IFS= read -r script; do
bash "$script" -H || break
done
And in parallel you have to get out of the pipe subshell: 同时,您必须退出管道子外壳:
while IFS= read -r script; do
bash "$script" -H || break &
done < <(
find . -name '*.sh' | head -n 10
)
wait # for all the childs
or wait for childs in the subshell itself: 或在子外壳本身中等待孩子:
find . -name '*.sh' | head -n 10 |
{
while IFS= read -r script; do
bash "$script" -H || break &
done
wait
}
From comments, keeping a count of process run 通过评论,保持流程运行的计数
count=0
for f in *.sh; do
bash "$f" -H || break
((++count>=3)) && break
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.