Created
November 15, 2022 19:49
-
-
Save douglascayers/b4d9391479765c3ce2bacb5e5fba9c48 to your computer and use it in GitHub Desktop.
Bash FOR loop with batched concurrency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# https://unix.stackexchange.com/questions/103920/parallelize-a-bash-for-loop | |
NAMES=($(cat names.csv)) | |
BATCH_SIZE=4 | |
for NAME in ${NAMES[@]}; do | |
# Increment or reset our counter | |
((i = i % $BATCH_SIZE)) | |
# The 'wait' pauses until all background commands are done | |
((i++ == 0)) && wait | |
# The '&' runs the command in background, non-blocking | |
some-cmd --some-args & | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment