Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 43 |
Nodes: | 6 (0 / 6) |
Uptime: | 97:16:02 |
Calls: | 290 |
Files: | 904 |
Messages: | 76,468 |
# Copyright 2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# @ECLASS: eapi9-pipestatus.eclass
# @MAINTAINER:
# Ulrich Müller <ulm@gentoo.org>
# @AUTHOR:
# Ulrich Müller <ulm@gentoo.org>
# @SUPPORTED_EAPIS: 7 8
# @BLURB: test the PIPESTATUS array
# @DESCRIPTION:
# A stand-alone implementation of a possible future pipestatus command
# (which would be aimed for EAPI 9). It is meant as a replacement for
# "assert". In its simplest form it can be called like this:
#
# @CODE
# foo | bar
# pipestatus || die
# @CODE
#
# With the -v option, the command will also echo the pipe status array,
# so it can be assigned to a variable like in the following example:
#
# @CODE
# local status
# foo | bar
# status=$(pipestatus -v) || die "foo | bar failed, status ${status}"
# @CODE
case ${EAPI} in
7|8) ;;
*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac
# @FUNCTION: pipestatus
# @USAGE: [-v]
# @RETURN: last non-zero element of PIPESTATUS, or zero if all are zero
# @DESCRIPTION:
# Test the PIPESTATUS array, i.e. the exit status of the command(s)
# in the most recently executed foreground pipeline. If called with
# option -v, also output the PIPESTATUS array.
pipestatus() {
local -a status=( "${PIPESTATUS[@]}" )
local s ret=0
[[ $# -gt 0 && ${1} != -v || $# -gt 1 ]] \
&& die "${FUNCNAME}: bad arguments: $@"
[[ ${1} == -v ]] && echo "${status[@]}"
for s in "${status[@]}"; do
[[ ${s} -ne 0 ]] && ret=${s}
done
return "${ret}"
}
On Sun, 24 Nov 2024, Michał Górny wrote:
# @CODE
# local status
# foo | bar
# status=$(pipestatus -v) || die "foo | bar failed, status ${status}"
I suppose you may want to put a verbose warning not to put "local"
on the same line, because people are going to do that as an "obvious" optimization.
[[ $# -gt 0 && ${1} != -v || $# -gt 1 ]] \
Please use parentheses when you combine && and ||, if only for the sake
of readability.
&& die "${FUNCNAME}: bad arguments: $@"
Replace the '\' with the '&&'.
[[ ${1} == -v ]] && echo "${status[@]}"
for s in "${status[@]}"; do
[[ ${s} -ne 0 ]] && ret=${s}
I suppose it's just my C-foo talking and completely needless
optimization here, but it really itches me to iterate the array
backwards and return on the first match.
On Sun, 24 Nov 2024, Ulrich M├╝ller wrote:
This implements a pipestatus command, as discussed in bug 566342 [1]
and on IRC.
ΓòôΓöÇΓöÇΓöÇΓöÇ
Γòæ Tests the shell's pipe status array, i.e. the exit status of the
Γòæ command(s) in the most recently executed foreground pipeline.
Γòæ Returns shell true (0) if all elements are zero, or the last
Γòæ non-zero element otherwise. If called with -v as the first argument,
Γòæ also outputs the pipe status array as a space-separated list. ΓòÖΓöÇΓöÇΓöÇΓöÇ
I looked forward to use pipestatus() in texlive-modules.eclass [1], but since the pipe includes a 'grep', which is not uncommon, I can not use
what is currently proposed.
On Wed, 27 Nov 2024, Florian Schmaus wrote:
╓────
║ Tests the shell's pipe status array, i.e. the exit status of the
║ command(s) in the most recently executed foreground pipeline.
║ Returns shell true (0) if all elements are zero, or the last
║ non-zero element otherwise. If called with -v as the first argument,
║ also outputs the pipe status array as a space-separated list.
╙────
Thanks again for putting effort into this ulm.
When this was discussed in #-qa one initial version of pipestatus()
had support for specifying non-zero exit statues as success indication
for certain commands in the pipe. The prime example being 'grep'
returning 1 if no input matched.
One proposed version had support for this, but it was removed in later version and the discussion in #-qa shifted towards how the value of PIPESTATUS can be preserved to be included in a potential error
message.
I agree that we should drop 'assert' and that dropping it requires a
sensible named alternative. And the proposed version of pipestatus()
is a functional equivalent alternative.
However, I am not sure why the proposed pipestatus() does no longer
include support for specifying non-zero exit statuses as success
indication.
I looked forward to use pipestatus() in texlive-modules.eclass [1],
but since the pipe includes a 'grep', which is not uncommon, I can not
use what is currently proposed.
It seems strange to me to go this far, but then drop the ball on the
last meter.
Could we please consider re-adding support for this?