diff options
author | Eric van Gyzen <vangyzen@FreeBSD.org> | 2020-10-01 21:48:22 +0000 |
---|---|---|
committer | Eric van Gyzen <vangyzen@FreeBSD.org> | 2020-10-01 21:48:22 +0000 |
commit | 63c8336d4de15085d50c9d8c855cdc97ee018a04 (patch) | |
tree | 3d5efee44aa41a41cebb8a303dc2ab96abfff9eb /usr.bin/grep/zgrep.sh | |
parent | 36972ee3e0fe8474e85646d0a2f1de9247a1e66a (diff) | |
download | src-63c8336d4de15085d50c9d8c855cdc97ee018a04.tar.gz src-63c8336d4de15085d50c9d8c855cdc97ee018a04.zip |
zgrep: fix exit status with multiple files
zgrep should exit with success when given multiple files and the
pattern is found in at least one file. Prior to this change,
it would exit with success only if the pattern was found in _every_ file.
Reviewed by: dab ngie
MFC after: 2 weeks
Sponsored by: Dell EMC Isilon
Differential Revision: https://reviews.freebsd.org/D26616
Notes
Notes:
svn path=/head/; revision=366345
Diffstat (limited to 'usr.bin/grep/zgrep.sh')
-rwxr-xr-x | usr.bin/grep/zgrep.sh | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/usr.bin/grep/zgrep.sh b/usr.bin/grep/zgrep.sh index acbcb48770a7..7e44c7053492 100755 --- a/usr.bin/grep/zgrep.sh +++ b/usr.bin/grep/zgrep.sh @@ -157,28 +157,35 @@ then pattern_found=1 fi -ret=0 # call grep ... if [ $# -lt 1 ] then # ... on stdin if [ ${pattern_file} -eq 0 ]; then - ${cattool} ${catargs} - | ${grep} ${grep_args} -- "${pattern}" - || ret=$? + ${cattool} ${catargs} - | ${grep} ${grep_args} -- "${pattern}" - else - ${cattool} ${catargs} - | ${grep} ${grep_args} -- - || ret=$? + ${cattool} ${catargs} - | ${grep} ${grep_args} -- - fi + ret=$? else # ... on all files given on the command line if [ ${silent} -lt 1 -a $# -gt 1 ]; then grep_args="-H ${grep_args}" fi + # Succeed if any file matches. First assume no match. + ret=1 for file; do if [ ${pattern_file} -eq 0 ]; then ${cattool} ${catargs} -- "${file}" | - ${grep} --label="${file}" ${grep_args} -- "${pattern}" - || ret=$? + ${grep} --label="${file}" ${grep_args} -- "${pattern}" - else ${cattool} ${catargs} -- "${file}" | - ${grep} --label="${file}" ${grep_args} -- - || ret=$? + ${grep} --label="${file}" ${grep_args} -- - + fi + this_ret=$? + # A match (0) overrides a no-match (1). An error (>=2) overrides all. + if [ ${this_ret} -eq 0 -a ${ret} -eq 1 ] || [ ${this_ret} -ge 2 ]; then + ret=${this_ret} fi done fi |