Click to See Complete Forum and Search --> : coprocess in bash


stiles
06-12-2002, 10:09 PM
Ok in Linux in a Nutshell there is the syntax for coprocess in BASH:


cmd1 | cmd2 |&

read -p var #Read coprocess input into variable var
print -p string #Write string to the coprocess


OK, I've only used coprocess in KSH, so the read / print is peachy, but what's with cmd1 | cmd2 |&

In KSH93 you just start up a process in the background that has a pipeline to the current shell like so:

sqlplus -s ${ora_user}/${ora_passwrd} |&

So has anybody seen any usage of coprocess in BASH, have a code snipit to share? Links or whatnot?

l01yuk
06-18-2002, 03:18 AM
"|&" is shorthand for "2>&1 |". So the book does not have the complete answer. Basically, from what you said, KSH seems to interpret an open ended pipe passing both stderr and stdout as a coprocess. Bash does not.

To become a co-process the pipe needs to be preceeded by coproc. You then read or write to the coproc using the read -p, write -p or using <&p, >&p.

Hope this helps.

l01yuk
06-18-2002, 04:16 AM
Ok, I sem to have got my shells mixed up, that is for zsh. Sorry. <feeling very stupid :(>

I think that in BASH you need to use named pipes.

mkfifo pipe
ls -l > pipe &
cat < pipe

Not really the same thing though.

stiles
06-18-2002, 08:01 PM
well I don't think BASH does coprocess. I did find this (http://www.linuxselfhelp.com/gnu/gawk/html_chapter/gawk_12.html#SEC159) rather cryptic bit of info on how to use gawk to set up coprocess. I don't know jack about gawk.

dchidelf
06-19-2002, 09:11 PM
I think all that "cmd1 | cmd2 |&" means is that a coprocess can be a pipeline of commands, not only a single command.
It could be
cmd1 |&
cmd1 | cmd2 |&
cmd1 | cmd2 | cmd3 |&
etc.
print -p writes to cmd1
and
read -p reads from the last cmd in the pipeline.

Using named pipes an equivalentish sort of thing might be
mkfifo inpipe
mkfifo outpipe
cmd1 <inpipe >outpipe &
- or -
cmd1 <inpipe | cmd2 | cmd3 >outpipe &
print string >inpipe
cat outpipe

That doesn't quite work the same, but it is sort of close... :)

Strogian
06-19-2002, 10:25 PM
Anyone know what this |& thing is supposed to mean? It's not mentioned anywhere in the bash man page, AFAIK.

dchidelf
06-20-2002, 07:03 PM
AFAIK it is a ksh thing... maybe it's been added to bash, but I have seen the exact description of usage:

cmd1 | cmd2 |&
read -p var #Read coprocess input into variable var
print -p string #Write string to the coprocess

in a description of coprocesses in ksh. Even the comments are identical.

So, I think that usage pertains to ksh.

Strogian
06-20-2002, 10:06 PM
Ah ok. ;)

stiles
06-21-2002, 03:22 PM
dchidelf, thanks for the 411 on using named pipes. I think using two pipes (or maybe even three if I need to trap for the stderr) will do almost the exact same thing as a KSH coprocess. Actually using named pipes may be better cause the stderr of the background process in ksh's coprocess propagates back to the forground shell in the same pipeline and the stdout.

bigrigdriver
06-21-2002, 06:41 PM
A pipeline is a list of simple commands seperated by the pipe symbol '|'. The cmd1 |cmd2 you refer to is simply a list (pipeline) of simple commands. If a command is terminated by the control character '&', the shell executes the command asynchronously in a subshell. This is known as executing the command in the BACKGROUND. Source: Bash Reference Manual 'Lists of Commands'.

[ 21 June 2002: Message edited by: bigrigdriver ]

Strogian
06-21-2002, 08:34 PM
Originally posted by bigrigdriver:
<STRONG>A pipeline is a list of simple commands seperated by the pipe symbol '|'. The cmd1 |cmd2 you refer to is simply a list (pipeline) of simple commands. If a command is terminated by the control character '&', the shell executes the command asynchronously in a subshell. This is known as executing the command in the BACKGROUND. Source: Bash Reference Manual 'Lists of Commands'.

[ 21 June 2002: Message edited by: bigrigdriver ]</STRONG>

Right. There is a pipe immediately preceding the &, though. (i.e. it would be piping it off to nowhere, if it were bash)