tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: sh: killing a pipe head from the tail



> In a similar situation I have written a script like this,

> (
> 	envstat -i 10 & echo $! > envstat.pid
> ) | while read ...
> 	...
> 	... || kill $(cat envstat.pid) && exit 1
> 	...
> done

...which, besides the issues you point out, depends on having a
writable temporary file available, and (especially if its name is fixed
as in the above) has potential issues with multiple runs colliding.  It
also has the risk that envstat will die and something else takes its
PID before the kill happens.  (In the case at hand that particular risk
is low-to-zero, but in other cases it may not be.)

> Usually it's necessary to protect against the loop racing against the
> creation of envstat.pid.  Seems like there should be a better way.

There should.

Most shells I'm aware of don't provide any way to describe it, but
there's no reason a shell couldn't provide a way to replace the file
with a pipe.  One of my very-spare-time projects is to design and build
a shell that's capable of significantly more elaborate pipelines.  I
have been thinking of pipelines more like

       +-----------+                 +-----------+
       |           > -- something -- >           |
in --- > ppmtorgb3 > -- something -- > rgb3toppm > --- out
       |           > -- something -- >           |
       +-----------+                 +-----------+

(where the "something"s could be, eg, pgmedge, or different processing
for each primary, or some such).  Or circular pipelines, which seem
hard to come up with a use case for until you think that one process
could be ssh, or nc, or the like.

But I'd like it to also be capable of something like this, of piping
the PID from the echo to the cat.

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse%rodents-montreal.org@localhost
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index