zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zyx.vim@gmail.com, zsh-workers@zsh.org
Subject: Re: `jobs' builtin does not work with pipe in scripts
Date: Sat, 27 Nov 2010 17:37:18 -0800	[thread overview]
Message-ID: <101127173718.ZM1906@torch.brasslantern.com> (raw)
In-Reply-To: <201011262116.15270@-zyx>

On Nov 26,  9:16pm, ZyX wrote:
}
} Reply to message «Re: `jobs' builtin does not work with pipe in scripts», 
} sent 20:47:39 26 November 2010, Friday
} by Bart Schaefer:
} 
} 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.


  reply	other threads:[~2010-11-28  1:37 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-25 20:41 ZyX
2010-11-25 20:57 ` Marc Weber
2010-11-25 21:40   ` ZyX
2010-11-25 23:56     ` Marc Weber
2010-11-26  4:38       ` ZyX
2010-11-26 17:47         ` Bart Schaefer
2010-11-26 18:16           ` ZyX
2010-11-28  1:37             ` Bart Schaefer [this message]
2010-11-28  3:26               ` ZyX

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=101127173718.ZM1906@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    --cc=zyx.vim@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).