相当于破折号外壳中的pipefail

Lav*_*vya 25 bash posix pipe dash-shell

是否有一些类似的选项dash对应于外壳pipefailbash

或者,如果管道中的一个命令失败(但没有在其上退出,set -e那么)获得非零状态的任何其他方式.

为了更清楚,这是我想要实现的一个例子:

在调试makefile示例中,我的规则如下所示:

set -o pipefail; gcc -Wall $$f.c -o $$f 2>&1 | tee err; if [ $$? -ne 0 ]; then vim -o $$f.c err; ./$$f; fi;
Run Code Online (Sandbox Code Playgroud)

基本上它运行会在出错时打开错误文件和源文件,并在没有错误时运行程序.给我一些打字.上面的代码段运行良好,bash但我的新Ubunty系统使用dash似乎不支持pipefail选项.

如果下面一组命令的第一部分失败,我基本上想要一个FAILURE状态:

gcc -Wall $$f.c -o $$f 2>&1 | tee err
Run Code Online (Sandbox Code Playgroud)

所以我可以用它来if表达.

有没有其他方法可以实现它?

谢谢!

Sta*_*iel 9

我遇到了同样的问题,并且在我正在使用的 docker 映像上的 dash shell (/bin/sh) 中,set -o pipefail和的bash 选项都失败了。${PIPESTATUS[0]}我不想修改图像或安装另一个包,但好消息是使用命名管道对我来说非常有效 =)

mkfifo named_pipe
tee err < named_pipe &
gcc -Wall $$f.c -o $$f > named_pipe 2>&1
echo $?
Run Code Online (Sandbox Code Playgroud)

请参阅此答案以了解我在哪里找到信息:https ://stackoverflow.com/a/1221844/431296


agc*_*agc 5

Q. 的示例问题要求:

如果...组命令的第一部分失败,我基本上想要一个 FAILURE 状态:

安装moreutils,并尝试util,它返回管道中第一个mispipe命令的退出状态:

sudo apt install moreutils
Run Code Online (Sandbox Code Playgroud)

然后:

if mispipe "gcc -Wall $$f.c -o $$f 2>&1" "tee err" ; then \
     ./$$f 
else 
     vim -o $$f.c err 
fi
Run Code Online (Sandbox Code Playgroud)

虽然 'mispipe' 在这里完成了这项工作,但它并不完全复制 shellbashpipefail;从man mispipe

   Note that some shells, notably bash, do offer a
   pipefail option, however, that option does not
   behave the same since it makes a failure of any
   command in the pipeline be returned, not just the
   exit status of the first.
Run Code Online (Sandbox Code Playgroud)