Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
RE: why does "jobs | wc" not work?
- X-seq: zsh-users 5538
- From: Borzenkov Andrey <Andrey.Borzenkov@xxxxxxxxxxx>
- To: "'dominik.vogt@xxxxxx'" <dominik.vogt@xxxxxx>,   "'zsh-users@xxxxxxxxxx'" <zsh-users@xxxxxxxxxx>
- Subject: RE: why does "jobs | wc" not work?
- Date: Tue, 26 Nov 2002 20:11:37 +0300
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
> 
> 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
bor@itsrm2% sleep 1000000 &
[1] 20639
bor@itsrm2% echo $#jobstates 
1
bor@itsrm2% echo $ZSH_VERSION 
4.0.4
-andrey
 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