help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] When pipes fail (and when not)


From: Bob Proulx
Subject: Re: [Help-bash] When pipes fail (and when not)
Date: Wed, 28 Nov 2018 14:16:02 -0700
User-agent: Mutt/1.10.1 (2018-07-13)

Chet Ramey wrote:
> Paul Wagner wrote:
> > but installing another trap like
> > 
> > trap 'echo foo >&2' pipe
> > for i in {0..9}; do echo $i; sleep 1; done | dd bs=1 count=10
> > 
> > ends after 5 iterations (so SIGPIPE is not trapped, but handled again), but
> > does not write 'foo' to stderr (the terminal), and I don't understand why.

Using echo as in the above is the bash builtin echo which doesn't
print diagnostics.  You would need to use an external standalone
command such as /bin/echo or one you write yourself that prints
diagnostic messages.

> Each element of a pipeline is executed in a subshell environment, described
> in
> 
> http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_12
> 
> "A subshell environment shall be created as a duplicate of the shell
> environment, except that signal traps that are not being ignored shall be
> set to the default action. [...] Additionally, each command of a multi-
> command pipeline is in a subshell environment; as an extension, however,
> any or all commands in a pipeline may be executed in the current
> environment."

As I read that it appears to give a solution.  It says that if a
signal is not being ignored then it will be set back to the default.
Therefore if we set a SIGPIPE handler to anything trivial but not
ignore and not default then that would make it not be ignored and
therefore should be set back to the default SIG_DFL.  But that does
not seem to be true.

> Subshell environments don't inherit traps from the parent shell, unless
> the trap has specified that the signal is to be ignored. Bash always
> executes the first element of a pipeline in a subshell environment.
> 
> If you define the trap in the same subshell environment as the for loop,
> you'll get the output you want.

  trap 'echo foo 1>&2' PIPE  # set a PIPE handler so it is not ignored

But I can't create any example which provides for resetting SIGPIPE
handling back to the original SIG_DFL in a child of a process where
the parent had previously ignored it.  I recall this always having
been the case forever for shells.

Perhaps in the above the "traps that are not being ignored shall be
set to the default action" really means set back to the previous
action?  Where the previous action was that it was ignored?

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]