How to implement 'set -o pipefail' in a POSIX way - almost done, expert help needed

后端 未结 4 653
故里飘歌
故里飘歌 2021-02-14 23:02

I have to implement the BASH set -o pipefail option in a POSIX way so that it works on various LINUX/UNIX flavors. To explain a bit, this option enables the user to

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-14 23:53

    The core of your idea should probably involve something like this:

    { cmd1 ; echo $? > status1 ; } | cmd2 && grep -q '^0$' status1 }
    

    In longer form, that would be:

    { cmd1 ; echo $? > status1 ; } |  \
    { cmd2 ; echo $? > status2 ; } |  \
      # ... and so on                 \
      cmdN                         && \
      # ^ note lack of wrapper        \ 
    grep -q '^0$' status1 &&          \
    grep -q '^0$' status2 &&          \
      # ... and so on, to N-1
    

提交回复
热议问题