3

I have a gawk program that uses a coprocess. However, sometimes I don't have any data to write to the coprocess, and my original script hangs while waiting for the output of the coprocess.

The code below reads from STDIN, writes each line to a "cat" program, running as a coprocess. Then it reads the coprocess output back in and writes it to STDOUT. If we change the if condition to be 1==0, nothing gets written to the coprocess, and the program hangs at the while loop.

From the manual, it seems that the coprocess and the two-way communication channels are only started the first time there is an IO operation with the |& operator. Perhaps we can start things without actually writing anything (e.g. writing an empty string)? Or is there a way to check if the coprocess ever started?

#!/usr/bin/awk -f
BEGIN {
    cmd = "cat"
##  print "" |& cmd
}

{
    if (1 == 1) {
       print |& cmd
    } 
}

END {
    close (cmd, "to")
    while ((cmd |& getline line)>0) {
       print line
    }
    close(cmd)
}
DavidR
  • 810
  • 2
  • 8
  • 16
  • For those reading the question, I suggested tag "gawk" to be synonym of "awk". It was an error, so feel free to downvote the request in http://stackoverflow.com/tags/awk/synonyms so that it gets revoked. Thanks! – fedorqui Jun 05 '14 at 17:23
  • I have some questions... what are you actually trying to achieve, please? What are `out` and `"to"`? You seem to close the coprocess befoe trying to read its output - doesn't seem sensible? – Mark Setchell Jun 05 '14 at 17:43
  • @MarkSetchell He only closes the "to" end of the process (before reading from the "from" end) Looks like you need to read the gawk manual! @DavidR My questions: What do you expect "cat" to output? What do you think `out` means? What are you trying to achieve with the superfluous `1 == 1`? – ooga Jun 05 '14 at 17:46
  • Can you just test the same condition used to print to the coprocess at the start of the while loop? Something like `while( condition && ((cmd |&... )` – n0741337 Jun 05 '14 at 17:48
  • @ooga,@MarkSetchell the "out" was a mistake. I've edited the code now. There is no point to the 1==1, except so that I could try to explain that if the line doesn't run (e.g. by changing the condition to 1==0), then the while loop hangs. – DavidR Jun 05 '14 at 19:43

1 Answers1

4

Great question, +1 for that!

Just test the return code of the close(cmd, "to") - it will be zero if the pipe was open, -1 (or some other value) otherwise. e.g.:

if (close(cmd, "to") == 0) {
   while ((cmd |& getline line)>0) {
      print line
   }
   close(cmd)
}
Ed Morton
  • 188,023
  • 17
  • 78
  • 185