From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24529 invoked by alias); 28 Nov 2010 01:37:55 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 28463 Received: (qmail 24504 invoked from network); 28 Nov 2010 01:37:43 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <101127173718.ZM1906@torch.brasslantern.com> Date: Sat, 27 Nov 2010 17:37:18 -0800 In-reply-to: <201011262116.15270@-zyx> Comments: In reply to ZyX "Re: `jobs' builtin does not work with pipe in scripts" (Nov 26, 9:16pm) References: <201011252341.44512@-zyx> <201011260738.12563@-zyx> <101126094739.ZM25971@torch.brasslantern.com> <201011262116.15270@-zyx> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zyx.vim@gmail.com, zsh-workers@zsh.org Subject: Re: `jobs' builtin does not work with pipe in scripts MIME-version: 1.0 Content-type: text/plain; charset=iso-8859-1 Content-transfer-encoding: quoted-printable On Nov 26, 9:16pm, ZyX wrote: } } Reply to message =ABRe: `jobs' builtin does not work with pipe in scripts= =BB,=20 } sent 20:47:39 26 November 2010, Friday } by Bart Schaefer: }=20 } What is your version of zsh? I tested using the latest from CVS: Src/zsh -fc 'print $ZSH_VERSION $ZSH_PATCHLEVEL' 4.3.10-dev-2 1.5130 However, I just also tried 4.2.0 and although -omonitor does not work, setopt monitor appears to; with the caveat that "jobs -p | ..." caused the shell to hang, but "... <(jobs -p)" worked. Hmm, previously you reported yours as "zsh-4.3.10-r2" but I can't find evidence that this was ever an official zsh version number. There were a bunch of changes to the handling of MONITOR in July 2009, which is a bit after 4.3.10 was released. That's also around the time that the POSIX_JOBS option was added, and is the last time job control was being changed in any noticeable way. } It does not work for me: } (zyx:~) % zsh -fc 'setopt monitor;sleep 3 & fg' } zsh:setopt:1: can't change option: monitor That should only happen if the shell has no terminal, and can't get one e.g. by opening /dev/tty. (I'm not sure that a terminal should still a requirement for MONITOR given its other evolution.) } And if zsh does not provide job control without this option, why } `jobs' works as expected without pipe? It's a question of whether the "jobs" command is running in a forked subshell or not. Subshells are incapable of manipulating the jobs created by their parent, so normally the job table is emptied just after forking when a subshell is begun. Zsh forks the left side of pipelines, so in "jobs | ..." there are no jobs to print. A special trick was put in place back in 2002 to allow the parent's job table to hang around specifically for the "jobs" command to be able to peek at it, but that read-only copy is only maintained when the MONITOR option is on. (That may again be an obsolete detail.) However, depending on what you want to do with the output, there are two other ways to go about this. One is to use the $jobstates hash from the zsh/parameter module, which maps job numbers to strings that describe the state. Another is to use "jobs -p >>(...)" to keep the jobs command in the foreground shell and manipulate its output in a subshell. An third is to direct output from jobs into a file, then read the file.