>>>>> 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. Thank you for the feedback. I have added the following: # Caveat: "pipestatus" must be the next command following the pipeline. # In particular, the "local" declaration must be before the pipeline, # otherwise it will reset the status. >> [[ $# -gt 0 && ${1} != -v || $# -gt 1 ]] \ > Please use parentheses when you combine && and ||, if only for the sake > of readability. I've replaced it by: [[ $# -gt 0 && ( ${1} != -v || $# -gt 1 ) ]] Note that [[ ( $# -gt 0 && ${1} != -v ) || $# -gt 1 ]] would also work, i.e. order doesn't matter (the reason is that "-gt 1" implies "-gt 0"). >> && die "${FUNCNAME}: bad arguments: $@" > Replace the '\' with the '&&'. We don't have any policy on this, and IMHO it is clearer to split the line before the operator. (This is also what the GNU coding standards say, and what is used in mathematical typesetting.) >> [[ ${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. I had considered this and decided against it, see the last paragraph of https://bugs.gentoo.org/566342#c13: | Alternatively, one could loop backwards over the array (and return | for the first nonzero status) but it would be more complicated. | I don't think that would be more efficient because the normal case | is that all statūs are zero. Plus, pipelines rarely consist of more | than 3 commands. Ulrich
signature.asc
Description: PGP signature