Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: why does "jobs | wc" not work?



On Tue, Nov 26, 2002 at 01:05:21PM +0000, Peter Stephenson wrote:
> Dominik Vogt wrote:
> > From a user's point of view I think it would be better to fork the
> > right hand side of the pipe.  This way, the left hand side always
> > generates the same output, regardless of the context in which it
> > is used.
> 
> If you look in the completion system, you will see various places where
> we do stuff on the lines of
> 
>   output_some_values | while read line; ...
> 
> in order to process values for the current shell, and very few where we
> need to output transient data from shell builtins.  I think jobs is the
> only common case.

Yes, I noticed that in the mean time.  Actually, all I am trying
to do is fetch the number of background jobs in sh without calling
external commands and - preferrably - not writing temporary data
to files.  It's surprisingly difficult to do:

  I=0; jobs | while read FOO; do I=$[I+1]; done; echo $I

works neither in zsh (jobs produces no output) nor in sh (the I
variable is local to the subshell running "while").  In sh
(actually, bash in sh mode), I can assign the output of jobs to
a variable:

  JOBS=`jobs`

But that doesn't help because I see no way to get that as input
into the while loop without forking it into a subshell.  In zsh,
this should work:

  I=0; echo "$JOBS" | while read X; do I=$[I+1]; done; echo $I

But then,

  JOBS=`jobs`

fails :-/

All I can think of is to write the output in a temporary file:

  jobs > x; I=0; while read X; do I=$[I+1]; done < x; echo $I

Can anyone think of a more efficient way (speed does matter here)?

Bye

Dominik ^_^  ^_^




Messages sorted by: Reverse Date, Date, Thread, Author