Is it somehow possible to return if the first command in a pipeline failed? With the pipefail option the return value is carried over but the pipeline still runs till the last command.
So in this example read is executed at least once.
set -o pipefail
shopt -s lastpipe
function_() {
#I am doing read at least once even if somecmd failed
somecmd |
while IFS=':' read -r line || return; do
:
done
}
without saving it to a variable like so?
function_() {
#do something only if somecmd returned with 0
someout=$(somecmd) || return
while IFS=':' read -r line || return; do
:
done <<< "$someout"
}
question from:
https://stackoverflow.com/questions/65838729/return-if-first-command-in-pipe-failed 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…