Bash "Co-process" example


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash "Co-process" example
# 1  
Old 04-18-2004
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
Code:
#!/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 : )

Last edited by home_king; 04-18-2004 at 10:16 AM..
# 2  
Old 04-18-2004
What you are using would most often be termed "background processes".
# 3  
Old 04-18-2004
Quote:
Originally posted by Perderabo
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. Smilie
# 4  
Old 04-18-2004
Quote:
Originally posted by home_king
Well, it's the typical way to make "co-process" in Bash, isn't it?
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.
# 5  
Old 04-19-2004
Quote:
Originally posted by Perderabo
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:
Code:
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:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Explaining behaviour of sudo bash "$0" "$@";

I've found this script part on the stackoverflow: if ; then sudo bash "$0" "$@"; exit "$?"; fi I realized that sudo bash "$0" "$@"; is the only needed for me. But the strange thing happens when I move this line outside the IF statement: sudo bash "$0" "$@"; stops the... (9 Replies)
Discussion started by: boqsc
9 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

7. Red Hat

"service" , "process" and " daemon" ?

Friends , Anybody plz tell me what is the basic difference between "service" , "process" and " daemon" ? Waiting for kind reply .. .. (1 Reply)
Discussion started by: shipon_97
1 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Dummies Questions & Answers

Process seen in "ps aux" but not "top"

Hi, I have a process that can be seen after "ps aux" command. However when I do "top" command. This process cannot be seen. How can this happen? Is there anything wrong with my code? (3 Replies)
Discussion started by: monkfan
3 Replies
Login or Register to Ask a Question