Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 仪表板壳体中管道失效的等效值_Bash_Posix_Pipe_Dash Shell - Fatal编程技术网

Bash 仪表板壳体中管道失效的等效值

Bash 仪表板壳体中管道失效的等效值,bash,posix,pipe,dash-shell,Bash,Posix,Pipe,Dash Shell,与bash中的pipefail对应的dashshell中是否有类似的选项 或者,如果管道中的某个命令失败(但没有退出,set-e会退出),则通过任何其他方式获得非零状态 为了更清楚地说明这一点,以下是我想要实现的一个例子: 在示例调试生成文件中,我的规则如下所示: set -o pipefail; gcc -Wall $$f.c -o $$f 2>&1 | tee err; if [ $$? -ne 0 ]; then vim -o $$f.c err; ./$$f; fi; 基

bash
中的
pipefail
对应的
dash
shell中是否有类似的选项

或者,如果管道中的某个命令失败(但没有退出,
set-e
会退出),则通过任何其他方式获得非零状态

为了更清楚地说明这一点,以下是我想要实现的一个例子:

在示例调试生成文件中,我的规则如下所示:

set -o pipefail; gcc -Wall $$f.c -o $$f 2>&1 | tee err; if [ $$? -ne 0 ]; then vim -o $$f.c err; ./$$f; fi;
基本上,它会在出错时打开错误文件和源文件,并在没有错误时运行程序。节省了我打字的时间。上面的代码片段在
bash
上运行良好,但我较新的Ubunty系统使用了
dash
,它似乎不支持
pipefail
选项

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

gcc -Wall $$f.c -o $$f 2>&1 | tee err
这样我就可以将其用于
if
语句

有没有其他方法可以实现这一目标


谢谢

Q.的样本问题要求:

我基本上想要一个失败状态,如果。。。命令组失败:

gcc -Wall $$f.c -o $$f 2>&1 | tee err
安装moreutils,然后尝试
mispipe
util,它返回管道中第一个命令的退出状态:

sudo apt install moreutils
然后:


虽然“mispipe”在这里起作用,但它并不是与shell的
pipefail
完全相同;从
人工错误管道

   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.

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

mkfifo命名管道
三通错误<命名管道&
gcc-Wall$$f.c-o$$f>命名管道2>&1
回声$?

看到这个答案,我在哪里找到了信息:

为什么不干脆扔掉
tee
<代码>如果gcc-Wall$$f.c-o$$f>$$f.log 2>&1;然后是cat$$f.log/$$Felse vim-o$$f.c$$f.log;fi
(或者,在Ubunty上安装
bash
。这只是一个合适的选择。)@rici谢谢!我之所以使用tee,是因为我还希望将stderr输出打印到屏幕上——主要是为了查看是否有任何警告。但如果没有其他办法,我可能会转而接受你的建议。关于安装bash:我假设(没有任何研究)dash是bash的一个进步,所以我不想切换回bash,但现在谷歌搜索一下它似乎不一定如此。我会多读一点关于差异的内容,然后再做决定。再次感谢您的指点!PIPESTATUS/pipefail的POSIX实现的“库存”答案是,我希望上面提到的set-e能为我提供与set-o pipefail相当的破折号,但事实并非如此:
$dash-c'set-e;假|猫|;回声$?0$
值得后人注意的是,这将处理问题中的特定情况,但仍然不等同于
-o pipefail
。根据
mispipe
,如果第一个流水线命令失败,mispipe将返回失败的错误代码<代码>-o如果任何流水线命令失败,bash中的pipefail
将失败。
mkfifo named_pipe
tee err < named_pipe &
gcc -Wall $$f.c -o $$f > named_pipe 2>&1
echo $?