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

后端 未结 4 1843
后悔当初
后悔当初 2021-02-14 22:59

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:30

    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?

提交回复
热议问题