7

Ok, obviously I am NOT a bash guru and am in need of one!

I have never used 'coproc' before, but it seems to be just what I need. But, I have to admit that I can not extrapolate from the various 'ping' examples out there! [I did try for a couple of hours...]

All I want to do is to start a 'coproc' shell script that can take input from standard in and writes it's results to standard out. I want the main script to do the sending and processing of those commands and results respectively.

Here is one of the simplest outlines of what I am trying to do: EDITED WITH BETTER DETAIL

#! /bin/bash

coproc bkgndProc {
    /some/path/to/usefulScript.sh  maybeSomeArgsHere
}

// send command #1 to bkgndProc here
result=$(echo 'command' <&${bkgndProc[0]})    ### Doesn't work for me
echo "Did it work? $result"   ### this just prints back the 'command' I used

// here execute conditional logic based on result:
// if result1; then
//     send command #2 here, getting results
// else
//     send command #3 here, again getting results
// fi

Sorry about using pseudo-code above, but I'm not sure what those sending commands should be! If anyone could supply the details that would be greatly appreciated!

JoeG
  • 7,191
  • 10
  • 60
  • 105

1 Answers1

8
result = $(echo 'command' <&${bkgndProc[0]})    ### Doesn't work for me

wouldn't work at least basically since you have spaces on it

result=$(echo 'command' <&${bkgndProc[0]})

---- Update ----

A simple concept could be shown in a script like this:

#!/bin/bash

# create the co-process
coproc myproc {
    bash
}

# send a command to it (echo a)
echo 'echo a' >&"${myproc[1]}"

# read a line from its output
read line <&"${myproc[0]}"

# show the line
echo "$line"

Outputs:

a

Another which reads multiple lines using a timeout:

#!/bin/bash

coproc myproc {
    bash
}

# send a command to message 4 random numbers in 4 lines
echo 'echo "$RANDOM"; echo "$RANDOM"; echo "$RANDOM"; echo "$RANDOM"' >&"${myproc[1]}"

# keep reading the line until it times out
while read -t 1 -u "${myproc[0]}" line; do
    echo "$line"
done

Output:

17393
1423
8368
1782

If we use cat, it would no longer quit since the other end is still alive and connected, and EOF is not yet reached. It's the reason why we used timeouts.

cat <&"${myproc[0]}"
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • Good point, but I did not have that white space in the actual code I tried. Edited the post and cleaned that up. – JoeG Sep 10 '13 at 18:44
  • @Aleks-DanielJakimenko - yes you are right, I had 'real' code which I could not post. I should have taken the time to create a still real example that would illustrate my problem – JoeG Sep 10 '13 at 19:12
  • 1
    @konsolebox - THANKS - the problem I had was in fact when I use the 'cat' as you mentioned. There is existing output so that was the first thing I did, and never made it past that point! Thanks also for a very clear, powerful, and simple example! – JoeG Sep 10 '13 at 19:14