首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >grep与zgrep多个文件的退出状态差异

grep与zgrep多个文件的退出状态差异
EN

Unix & Linux用户
提问于 2019-09-05 18:54:45
回答 1查看 421关注 0票数 4

设置

代码语言:javascript
复制
echo "abc" >/tmp/foo1
echo "def" >/tmp/foo2
cp /tmp/foo1 /tmp/gzfoo1
cp /tmp/foo2 /tmp/gzfoo2
gzip /tmp/gzfoo*

多个文件的grep退出状态,一个匹配为0。

代码语言:javascript
复制
grep -q abc /tmp/foo[12]
echo $?
0

具有多个解压缩文件且匹配为1的zgrep退出状态

代码语言:javascript
复制
zgrep -q abc /tmp/foo[12]
echo $?
1

具有多个压缩文件且匹配为1的zgrep退出状态

代码语言:javascript
复制
zgrep -q abc /tmp/gzfoo[12].gz
echo $?
1

我确实看到zgrep是一个shell脚本。看起来,如果任何grep返回非零,zgrep也返回非零。以下是我从zgrep摘录的译文:

代码语言:javascript
复制
res=0
for input_file
do
  # ... run grep on input_file ...
  r=$?
  ...
  test $res -lt $r && res=$r
done
exit $res

zgrep版本为(古) 1.3.12:

代码语言:javascript
复制
$ zgrep --version
zgrep (gzip) 1.3.12
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License .
There is NO WARRANTY, to the extent permitted by law.

Written by Jean-loup Gailly.

zgrep (gzip) 1.6也会发生:

代码语言:javascript
复制
$ /.
There is NO WARRANTY, to the extent permitted by law.

Written by Jean-loup Gailly.

$ /

问题: zgrep中有bug吗?应该修好吗?

编辑:使用zgrep/gzip 1.8找到了一台更新的机器,它没有这个问题。看来我的机器太旧了。下面是它在新机器上的样子:

代码语言:javascript
复制
: zgrep --version
zgrep (gzip) 1.8
Copyright (C) 2010-2016 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License .
There is NO WARRANTY, to the extent permitted by law.

Written by Jean-loup Gailly.

: zgrep -q abc /tmp/foo[12]
: echo $?
0

为了避免旧的/错误的zgrep,你要做些简单的解决办法:

代码语言:javascript
复制
: ( gzcat -f /tmp/foo[12] | grep -q abc ) >&/dev/null 
: echo $?
0
EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2019-09-05 19:44:29

您可以从https://savannah.gnu.org/git/?group=gzip获得源代码。在提交d2a1928e5534017456dc8a3b600ba0b30cce4a6e中更改了返回代码:

代码语言:javascript
复制
commit d2a1928e5534017456dc8a3b600ba0b30cce4a6e
Author: Paul Eggert 
Date:   Thu Jun 12 18:43:08 2014 -0700

    zgrep: exit with status 0 if a file matches and there's no trouble

    Reported by Pavel Raiskup in: http://bugs.gnu.org/17760
    * zgrep.in (res): Treat exit status 0 to be greater than 1.
    Also, exit immediately on software configuration error.

提交消息包含一个指向错误报告的链接:https://debbugs.gnu.org/cgi/bugreport.cgi?bug=17760

你可以自己检查一下。使用上述提交构建的zgrep

代码语言:javascript
复制
$ /media/data/gzip-install-newer/bin/zgrep --version
zgrep (gzip) 1.6.17-d2a1
Copyright (C) 2010-2014 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License .
There is NO WARRANTY, to the extent permitted by law.

Written by Jean-loup Gailly.
$ /media/data/gzip-install-newer/bin/zgrep -q abc /tmp/foo[12]
$ echo $?
0

使用上一次提交构建的zgrep

代码语言:javascript
复制
$ /media/data/gzip-install/bin/zgrep --version
zgrep (gzip) 1.6.16-ed8c
Copyright (C) 2010-2014 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License .
There is NO WARRANTY, to the extent permitted by law.

Written by Jean-loup Gailly.
$ /media/data/gzip-install/bin/zgrep -q abc /tmp/foo[12]
$ echo $?
1
票数 2
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/539255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档