zsh-workers
 help / color / mirror / code / Atom feed
* `jobs' builtin does not work with pipe in scripts
@ 2010-11-25 20:41 ZyX
  2010-11-25 20:57 ` Marc Weber
  0 siblings, 1 reply; 9+ messages in thread
From: ZyX @ 2010-11-25 20:41 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 500 bytes --]

Compare the following two commands:
    sleep 1s & sleep 1s & jobs -p | while read line ; do echo $line ; done
and
    zsh -c 'sleep 1s & sleep 1s & jobs -p | while read line ; do echo $line ; done'
. First command (in interactive shell) works just fine and prints output of 
`jobs'. Second command prints nothing, while `jobs -p' without pipe works just 
fine (but is useless for me). How can this be fixed?

zsh-4.3.10-r2 (USE: doc, examples, gdbm, maildir, pcre, unicode), gentoo, amd64.

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: `jobs' builtin does not work with pipe in scripts
  2010-11-25 20:41 `jobs' builtin does not work with pipe in scripts ZyX
@ 2010-11-25 20:57 ` Marc Weber
  2010-11-25 21:40   ` ZyX
  0 siblings, 1 reply; 9+ messages in thread
From: Marc Weber @ 2010-11-25 20:57 UTC (permalink / raw)
  To: zsh-workers

Excerpts from ZyX's message of Thu Nov 25 21:41:43 +0100 2010:
>     zsh -c 'sleep 1s & sleep 1s & jobs -p | while read line ; do echo $line ; done'

What about zsh -i -c ' ...  ?

Marc Weber


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: `jobs' builtin does not work with pipe in scripts
  2010-11-25 20:57 ` Marc Weber
@ 2010-11-25 21:40   ` ZyX
  2010-11-25 23:56     ` Marc Weber
  0 siblings, 1 reply; 9+ messages in thread
From: ZyX @ 2010-11-25 21:40 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: Text/Plain, Size: 614 bytes --]

Reply to message «Re: `jobs' builtin does not work with pipe in scripts», 
sent 23:57:53 26 November 2010, Friday
by Marc Weber:

> Excerpts from ZyX's message of Thu Nov 25 21:41:43 +0100 2010:
> >     zsh -c 'sleep 1s & sleep 1s & jobs -p | while read line ; do echo
> >     $line ; done'
> 
> What about zsh -i -c ' ...  ?
It works, but I was trying to use `jobs -p | ...' in a script that is intended 
to be used as library, so I can't put something like `#!/bin/zsh -i' there. For 
now it was replaced with `ps --ppid $$', though I do not like invoking 
additional process in a signal handler.

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: `jobs' builtin does not work with pipe in scripts
  2010-11-25 21:40   ` ZyX
@ 2010-11-25 23:56     ` Marc Weber
  2010-11-26  4:38       ` ZyX
  0 siblings, 1 reply; 9+ messages in thread
From: Marc Weber @ 2010-11-25 23:56 UTC (permalink / raw)
  To: zsh-workers

ZyX,

I think you should provide more context. Its almost impossible to try to
find a workaround. Eg you could use /bin/sh

/bin/sh -c 'sleep 1s & sleep 1s & jobs | cat ' | while read .. ?

Anyway you seem to have found something which works for you.

Marc Weber


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: `jobs' builtin does not work with pipe in scripts
  2010-11-25 23:56     ` Marc Weber
@ 2010-11-26  4:38       ` ZyX
  2010-11-26 17:47         ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: ZyX @ 2010-11-26  4:38 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: Text/Plain, Size: 870 bytes --]

Reply to message «Re: `jobs' builtin does not work with pipe in scripts», 
sent 02:56:37 26 November 2010, Friday
by Marc Weber:

> I think you should provide more context. Its almost impossible to try to
> find a workaround. Eg you could use /bin/sh
> 
> /bin/sh -c 'sleep 1s & sleep 1s & jobs | cat ' | while read .. ?
> 
> Anyway you seem to have found something which works for you.
What more? `jobs -p | ...' is not working, I do not want to rewrite everything
in POSIX shell and I can't use interactive shell. `jobs -p | ...' is used in a
script that launches parallel processes and, when zsh catches CHLD, looks which
of them finished. You can see it here: http://vimpluginloader.hg.sourceforge.net/hgweb/vimpluginloader/dev-tools/file/tip/parjobs.zsh.
I do not like `ps --ppid $$' solution because it launches additional process,
but it works.

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: `jobs' builtin does not work with pipe in scripts
  2010-11-26  4:38       ` ZyX
@ 2010-11-26 17:47         ` Bart Schaefer
  2010-11-26 18:16           ` ZyX
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2010-11-26 17:47 UTC (permalink / raw)
  To: zyx.vim, zsh-workers

On Nov 26,  7:38am, ZyX wrote:
}
} What more? `jobs -p | ...' is not working

If you want "jobs" to do something reliable, you have to set the
MONITOR option.  Zsh by default does not provide job control in a
non-interactive shell.

% zsh -fc 'sleep 3 & fg'
zsh:fg:1: no job control in this shell.
% zsh -fc 'setopt monitor; sleep 3 & fg'
[1] 25945
[1]  + running    sleep 3
% zsh -fc 'sleep 3 & jobs -p | sed s/run/RUN/'
% zsh -o monitor -fc 'sleep 3 & jobs -p | s/run/RUN/'
[1] 25967
[1]  + 25967 RUNning    sleep 3
% 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: `jobs' builtin does not work with pipe in scripts
  2010-11-26 17:47         ` Bart Schaefer
@ 2010-11-26 18:16           ` ZyX
  2010-11-28  1:37             ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: ZyX @ 2010-11-26 18:16 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: Text/Plain, Size: 1216 bytes --]

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? It does not work for me:
    (zyx:~) % zsh -fc 'sleep 3 & fg'
    zsh:fg:1: no job control in this shell.
    (zyx:~) % zsh -omonitor -fc 'sleep 3 & fg'
    zsh:fg:1: no job control in this shell.
    (zyx:~) % zsh -fc 'setopt monitor;sleep 3 & fg'
    zsh:setopt:1: can't change option: monitor
    zsh:fg:1: no job control in this shell.
And if zsh does not provide job control without this option, why `jobs' works as 
expected without pipe?

Original message:
> On Nov 26,  7:38am, ZyX wrote:
> }
> } What more? `jobs -p | ...' is not working
> 
> If you want "jobs" to do something reliable, you have to set the
> MONITOR option.  Zsh by default does not provide job control in a
> non-interactive shell.
> 
> % zsh -fc 'sleep 3 & fg'
> zsh:fg:1: no job control in this shell.
> % zsh -fc 'setopt monitor; sleep 3 & fg'
> [1] 25945
> [1]  + running    sleep 3
> % zsh -fc 'sleep 3 & jobs -p | sed s/run/RUN/'
> % zsh -o monitor -fc 'sleep 3 & jobs -p | s/run/RUN/'
> [1] 25967
> [1]  + 25967 RUNning    sleep 3
> %

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: `jobs' builtin does not work with pipe in scripts
  2010-11-26 18:16           ` ZyX
@ 2010-11-28  1:37             ` Bart Schaefer
  2010-11-28  3:26               ` ZyX
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2010-11-28  1:37 UTC (permalink / raw)
  To: zyx.vim, zsh-workers

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.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: `jobs' builtin does not work with pipe in scripts
  2010-11-28  1:37             ` Bart Schaefer
@ 2010-11-28  3:26               ` ZyX
  0 siblings, 0 replies; 9+ messages in thread
From: ZyX @ 2010-11-28  3:26 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: Text/Plain, Size: 2139 bytes --]

Reply to message «Re: `jobs' builtin does not work with pipe in scripts», 
sent 04:37:18 28 November 2010, Sunday
by Bart Schaefer:

> 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.

-r2 in gentoo means that maintainer(s) had two times done something that 
requires package update, but have not changed mainstream version they are using. 
So zsh-4.3.10-r2 is zsh-4.3.10 with two ebuild updates. More information can be 
found here: http://devmanual.gentoo.org/general-concepts/ebuild-revisions/. 
Quote:
> Ebuilds may have a Gentoo revision number associated with them. This is a -rX
> suffix, where X is an integer — see File Naming Rules. This component must
> only be used for Gentoo changes, not upstream releases. By default, -r0 is
> implied. 
>
> Ebuilds should have their -rX incremented whenever a change is made which will
> make a substantial difference to what gets installed by the package — by
> substantial, we generally mean "something for which many users would want to
> upgrade". This is usually for bugfixes.
>
> Simple compile fixes do not warrant a revision bump; this is because they do
> not affect the installed package for users who already managed to compile it.
> Small documentation fixes are also usually not grounds for a new revision.


> 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.
Thanks, jobstates works just fine.

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-11-28  3:27 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-25 20:41 `jobs' builtin does not work with pipe in scripts 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
2010-11-28  3:26               ` ZyX

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).