Shell – How to use GNU Parallel for this while loop

filesgnu-parallelshell-script

So I have a while loop:

cat live_hosts | while read host; do \
    sortstuff.sh -a "$host" > sortedstuff-"$host"; done

But this can take a long time. How would I use GNU Parallel for this while loop?

Best Answer

You don't use a while loop.

parallel "sortstuff.sh -a {} > sortedstuff-{}" <live_hosts

Note that this won't work if you have paths in your live_hosts (e.g. /some/dir/file) as it would expand to sortstuff.sh -a /some/dir/file > sortedstuff-/some/dir/file (resulting in no such file or directory); for those cases use {//} and {/} (see gnu-parallel manual for details):

parallel "sortstuff.sh -a {} > {//}/sortedstuff-{/}" <live_hosts
Related Question