本文介绍了为什么要退出code 141使用grep -q?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释为什么我得到的退出code 141从下面?

Can someone explain why I get exit code 141 from the below?

#!/usr/bin/bash

set -o pipefail

zfs list | grep tank
echo a ${PIPESTATUS[@]}

zfs list | grep -q tank
echo b ${PIPESTATUS[@]}

cat /etc/passwd | grep -q root
echo c ${PIPESTATUS[@]}

我得到

...
a 0 0
b 141 0
c 0 0

从我的理解退出code 141是失败的,但上面的行给出零,所以它应该是成功的,我会说。

From my understanding exit code 141 is a failure, but the line above gives zero, so it should be success, I would say.

推荐答案

这是因为的grep -q 立即用零状态,一旦发现匹配退出。在 ZFS 命令仍然是写入管道,但没有读者(因为的grep 已退出),所以它从内核发送 SIGPIPE 信号,并将其与的状态退出 141

This is because grep -q exits immediately with a zero status as soon as a match is found. The zfs command is still writing to the pipe, but there is no reader (because grep has exited), so it is sent a SIGPIPE signal from the kernel and it exits with a status of 141.

如果您看到这个行为的另一个共同的地方是。例如。

Another common place where you see this behaviour is with head. e.g.

$ seq 1 10000 | head -1
1

$ echo ${PIPESTATUS[@]}
141 0

在这种情况下,读的第一线,终止其产生的 SIGPIPE 信号和 SEQ 141 退出。

In this case, head read the first line and terminated which generated a SIGPIPE signal and seq exited with 141.

请参阅从Linux的程序员指南。

See "The Infamous SIGPIPE Signal" from The Linux Programmer's Guide.

这篇关于为什么要退出code 141使用grep -q?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 18:14