I have a working piece of code to run all scripts in a directory: Run all shell scripts in folder
for f in *.sh; do \
bash "$f" -H || break
done
I also have code to run a sequence of .sh scripts:
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.
The order in which N files are run is not important to me.
find
the scripts, get the head
, then execute with xargs
.
find . -name '*.sh' | head -n 10 | xargs -n1 sh
You can run the scripts in parallel with xargs
with a simple -P0
option. You can script the xargs
with some xargs sh -c 'bash "$@" -H || exit 125' --
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.
If you feel like unfamiliar with xargs
, just do a simple while read
loop:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.