]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
MFC r344502: sh: Add set -o pipefail
authorJilles Tjoelker <jilles@FreeBSD.org>
Sun, 24 Mar 2019 22:10:26 +0000 (22:10 +0000)
committerJilles Tjoelker <jilles@FreeBSD.org>
Sun, 24 Mar 2019 22:10:26 +0000 (22:10 +0000)
commit3abaec56f008cfb5df35a392e3f826a25aff9ae8
tree87b5b09f7d8e3585cd761ebf903e0ce7287431b9
parent1f83fdcb408bbb51b93845b3158257b55b6eb5c8
MFC r344502: sh: Add set -o pipefail

The pipefail option allows checking the exit status of all commands in a
pipeline more easily, at a limited cost of complexity in sh itself. It works
similarly to the option in bash, ksh93 and mksh.

Like ksh93 and unlike bash and mksh, the state of the option is saved when a
pipeline is started. Therefore, even in the case of commands like
  A | B &
a later change of the option does not affect the exit status, the same way
  (A | B) &
works.

Since SIGPIPE is not handled specially, more work in the script is required
for a proper exit status for pipelines containing commands such as head that
may terminate successfully without reading all input. This can be something
like

(
cmd1
r=$?
if [ "$r" -gt 128 ] && [ "$(kill -l "$r")" = PIPE ]; then
exit 0
else
exit "$r"
fi
) | head

PR: 244270
Relnotes: yes
bin/sh/jobs.c
bin/sh/options.h
bin/sh/sh.1
bin/sh/tests/execution/Makefile
bin/sh/tests/execution/pipefail1.0 [new file with mode: 0644]
bin/sh/tests/execution/pipefail2.42 [new file with mode: 0644]
bin/sh/tests/execution/pipefail3.42 [new file with mode: 0644]
bin/sh/tests/execution/pipefail4.42 [new file with mode: 0644]
bin/sh/tests/execution/pipefail5.42 [new file with mode: 0644]
bin/sh/tests/execution/pipefail6.42 [new file with mode: 0644]
bin/sh/tests/execution/pipefail7.0 [new file with mode: 0644]