From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 514 invoked from network); 26 Nov 2002 13:33:22 -0000 Received: from sunsite.dk (130.225.247.90) by ns1.primenet.com.au with SMTP; 26 Nov 2002 13:33:22 -0000 Received: (qmail 12966 invoked by alias); 26 Nov 2002 13:32:48 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 5535 Received: (qmail 12935 invoked from network); 26 Nov 2002 13:32:43 -0000 Date: Tue, 26 Nov 2002 14:33:33 +0100 From: Dominik Vogt To: zsh-users@sunsite.dk Subject: Re: why does "jobs | wc" not work? Message-ID: <20021126133333.GI1937@gmx.de> Reply-To: dominik.vogt@gmx.de Mail-Followup-To: zsh-users@sunsite.dk References: <20021126124814.GH1937@gmx.de> <29376.1038315921@csr.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <29376.1038315921@csr.com> User-Agent: Mutt/1.3.28i 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 ^_^ ^_^