Category: BASH

BASH: pipe to functions

#!/bin/bash

function ProcessOneEntry() {

while read entry # Use the “read” function to do the trick
do
# Code here to do the job
done

}

# Application 1:
cat AnListFile | ProcessOneEntry

# Or
# Application 2:

find InputDirectory -name “Pattern” | ProcessOneEntry

Use xargs with functions

Example:

—————————-
worker () {
echo $1
}

export -f worker # This is important

cat inputList.txt | xargs -L 1 -I {} -P 4 bash -c “worker {}”
# Use bash -c “CommandToExecute”

—————————-

This trick can also be used with the find command.
find . -name ‘*.txt’ -exec bash -c “worker {}” \;
# Do not forget the “\;” termination for the -exec option of find.