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

后端 未结 4 654
故里飘歌
故里飘歌 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

    My two cents:

    #!/bin/sh
    
    # Saving the pid of the main shell is required,
    # as each element of the pipe is a subshell.
    self=$$
    
    lots_and_fail() {
        seq 100
        return 1
    }
    
    { lots_and_fail || kill $self; } | sed s/7/3/
    

    This thing seems to do the job. Thoughts?

提交回复
热议问题