Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

bash - return if first command in pipe failed

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...