Bash "Co-process" example

It copys one specified file to multiple "unique"(filter those different pathnames which in fact point to the same physical path) paths with parallelism in bash. In contrast to sequential copy processes, it performs well under multiprocessor systems.
The efficiency of Co-process is normally determined by three factors:
1.Whether CPU bound or not
2.Whether scientific data processing or not
3.Whether I/O or not


#!/bin/sh
# 
# Written by home_king <home_king@163.com>
# mcp---Multiple copy with Co-Process
# command format:
# mcp filename pathname1 pathname2 ... pathnameN
#
checkargs(){
        if [ $# -eq 0 ];then
                echo 'No arguments supplied!'
                return 1
        fi
        cpfile=$1
        if [ ! -f $cpfile ]; then
                echo 'Invaild file to copy!'
                return 1
        fi
        shift
        for tmparg in "$@"; do
                if [ ! -d $tmparg ];then
                        echo "There's at least one invalid directory"
                        return 1
                fi
        done
        argnums=($(echo $(stat -Lc %i $@) | awk -v inum=$(stat -c %i $PWD) '{for(i=1;i<=NF;i++){if($i == inum)continue;ldirs[$i]=i}} END{for(item in ldirs){print ldirs[item]}}'))
}

checkargs $@
[ $? -eq 0 ] || exit 1
shift

echo "Destination:"
for arg in "${argnums[@]}"; do
        eval echo '$'$arg
        eval cp -f $cpfile '$'$arg &
done
wait
echo ""
echo 'All done!'

Welcome to www.linuxsir.org/bbs : )

What you are using would most often be termed "background processes".

Well, it's the typical way to make "co-process" in Bash, isn't it? Maybe ksh would be better. But I just know Bash. :wink:

Well no, not at all. Here is the bash FAQ which is being maintained by the author of bash. According to bash's author, the recommended technique is "named pipe pairs (one for read, one for write)". Naturally, there would also be a process connected to the named pipes.

It is never said that "daemon+wait" is the only way to make "Co-process" in Bash, but the typical way.
What's more, conceptly, "named pipe" is different from "Co-process". The former depends some command's position parameters. Strictly speaking, it is called "Process Substitution".
Have a look at my example:

tar cf >(gzip -v9cf >dir.tar.gz) dir/

This commnad bound the input of gzip process with the first parameter(also tar's output destination) of tar process.
Another example:

cmp <(cat file1) <(cat file2)

It's not "Co-process"!
It's not the case that "named pipe pair"! The number of named pipes is restricted by the number of appropriate position parameters.
Please refer to ��Learning the bash shell��of O'reilly.