bash 相当于仪表板外壳中的 pipefail
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17757039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
equivalent of pipefail in dash shell
提问by Lavya
Is there some similar option in dashshell corresponding to pipefailin bash?
是否有一些类似的选项dash壳对应pipefail的bash?
Or any other way of getting a non-zero status if one of the commands in pipe fail (but not exiting on it which set -ewould).
或者如果管道中的一个命令失败(但不会退出它set -e),则获得非零状态的任何其他方式。
To make it clearer, here is an example of what I want to achieve:
为了更清楚,这是我想要实现的一个例子:
In a sample debugging makefile, my rule looks like this:
在示例调试 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;
Basically it runs opens the error file and source file on error and runs the programs when there is no error. Saves me some typing. Above snippet works well on bashbut my newer Ubunty system uses dashwhich doesn't seem to support pipefailoption.
基本上它运行时会打开错误文件和源文件,并在没有错误时运行程序。省了我打字的时间。上面的代码片段运行良好,bash但我较新的 Ubunty 系统使用的dash似乎不支持pipefail选项。
I basically want a FAILURE status if the first part of the below group of commands fail:
如果以下命令组的第一部分失败,我基本上想要一个 FAILURE 状态:
gcc -Wall $$f.c -o $$f 2>&1 | tee err
so that I can use that for the ifstatement.
这样我就可以将其用于if声明。
Are there any alternate ways of achieving it?
有没有其他方法可以实现它?
Thanks!
谢谢!
回答by agc
The Q.'s sample problem requires:
Q. 的样本问题需要:
I basically want a FAILURE status if the first partof the ... group of commands fail:
如果... 命令组的第一部分失败,我基本上想要一个 FAILURE 状态:
Install moreutils, and try the mispipeutil, which returns the exit status of the firstcommand in a pipe:
安装moreutils,并尝试使用mispipeutil,它返回管道中第一个命令的退出状态:
sudo apt install moreutils
Then:
然后:
if mispipe "gcc -Wall $$f.c -o $$f 2>&1" "tee err" ; then \
./$$f
else
vim -o $$f.c err
fi
While 'mispipe' does the job here, it is not an exact duplicate of the bashshell's pipefail; from man mispipe:
虽然 'mispipe' 在这里完成了这项工作,但它并不是bashshell 的pipefail;的完全重复。来自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.
回答by Stan Kurdziel
I ran into this same issue and the bash options of set -o pipefailand ${PIPESTATUS[0]}both failed in the dash shell (/bin/sh) on the docker image I'm using. I'd rather not modify the image or install another package, but the good news is that using a named pipe worked perfectly for me =)
我遇到了同样的问题和bash的选项set -o pipefail,并${PIPESTATUS[0]}都在仪表壳(/ bin / sh的),我使用的是搬运工的图像上失败。我宁愿不修改图像或安装另一个包,但好消息是使用命名管道对我来说非常有效 =)
mkfifo named_pipe
tee err < named_pipe &
gcc -Wall $$f.c -o $$f > named_pipe 2>&1
echo $?
See this answer for where I found the info: https://stackoverflow.com/a/1221844/431296
请参阅此答案以了解我在哪里找到信息:https: //stackoverflow.com/a/1221844/431296

