$ find . -name 'segment*' | xargs -n1 -P4 sh someFunction.sh
has the effect that at most four copies of the
someFunction.sh shell script will be started (
-P 4) in parallel (new ones will be spawed as soon as the old ones are done), each one getting one filename as its argument (
This means that each invocation of your script will look like
sh someFunction.sh segmentsomething
Inside the script, the shell will put the values of the positional parameters (the arguments on the command line) into
$2 etc. (
$0 usually contains the name of the script itself). In your case,
$1 will contain the name of the file, and the others will be empty.
So, in the script:
That's that. Now, usually when one uses
find to look for files and pass their filenames to
xargs there is the issue with wonky filenames that people tend to remind each other of, and I'll do that here too.
find utility passes filenames separated by whitespace. That's no good if you have filenames with spaces in them as that would cause problems for
xargs to invoke your script with proper names.
Therefore, it's good practice to always use
xargs, which means that the filenames, instead of being space-separated, are separated by
nul characters (
\0). This makes it a lot safer.
$ find . -name 'segment*' -print0 | xargs -0 -n1 -P4 sh someFunction.sh