设置
echo "abc" >/tmp/foo1
echo "def" >/tmp/foo2
cp /tmp/foo1 /tmp/gzfoo1
cp /tmp/foo2 /tmp/gzfoo2
gzip /tmp/gzfoo*
grep 退出状态有多个文件且一个匹配项为 0
grep -q abc /tmp/foo[12]
echo $?
0
zgrep 退出状态,有多个解压文件,其中一个匹配项为 1
zgrep -q abc /tmp/foo[12]
echo $?
1
zgrep 退出状态有多个压缩文件且一个匹配项为 1
zgrep -q abc /tmp/gzfoo[12].gz
echo $?
1
我确实看到 zgrep 是一个 shell 脚本。看起来确实好像如果任何grep 返回非零,zgrep 也返回非零。这是我从 zgrep 中转述的摘录:
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:
$ 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 <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.
Written by Jean-loup Gailly.
zgrep (gzip) 1.6 也会发生这种情况:
$ /<other_zgrep_path/bin/zgrep --version
zgrep (gzip) 1.6
Copyright (C) 2010-2013 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.
Written by Jean-loup Gailly.
$ /<other_zgrep_path/bin/zgrep -q abc /tmp/gzfoo[12].gz
$ echo $?
1
问题:zgrep 是否有错误?应该修复吗?
编辑:找到一台带有 zgrep/gzip 1.8 的较新机器,它没有这个问题。所以,看来我的机器很旧了。这是新机器上的样子:
: 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 <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.
Written by Jean-loup Gailly.
: zgrep -q abc /tmp/foo[12]
: echo $?
0
避免旧的/有缺陷的 zgrep 的 Hacky 解决方法:
: ( gzcat -f /tmp/foo[12] | grep -q abc ) >&/dev/null
: echo $?
0
答案1
您可以从以下位置获取源代码
https://savannah.gnu.org/git/?group=gzip。返回代码在提交中已更改d2a1928e5534017456dc8a3b600ba0b30cce4a6e
:
commit d2a1928e5534017456dc8a3b600ba0b30cce4a6e
Author: Paul Eggert <[email protected]>
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
上面的提交构建:
$ /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 <http://www.gnu.org/licenses/gpl.html>.
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
之前的提交构建:
$ /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 <http://www.gnu.org/licenses/gpl.html>.
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