Date: Thu, 11 Feb 2021 15:21:06 GMT From: Kyle Evans <kevans@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: 07c84bb078e1 - stable/12 - zgrep: fix exit status with multiple files Message-ID: <202102111521.11BFL6fq005543@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch stable/12 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=07c84bb078e1974be265fc921481ee1608684103 commit 07c84bb078e1974be265fc921481ee1608684103 Author: Eric van Gyzen <vangyzen@FreeBSD.org> AuthorDate: 2020-10-01 21:48:22 +0000 Commit: Kyle Evans <kevans@FreeBSD.org> CommitDate: 2021-02-11 15:19:13 +0000 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. (cherry picked from commit 63c8336d4de15085d50c9d8c855cdc97ee018a04) --- contrib/netbsd-tests/usr.bin/grep/t_grep.sh | 19 +++++++++++++++++++ usr.bin/grep/zgrep.sh | 17 ++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/contrib/netbsd-tests/usr.bin/grep/t_grep.sh b/contrib/netbsd-tests/usr.bin/grep/t_grep.sh index 75ee254cc79b..e094b15c6d67 100755 --- a/contrib/netbsd-tests/usr.bin/grep/t_grep.sh +++ b/contrib/netbsd-tests/usr.bin/grep/t_grep.sh @@ -891,6 +891,24 @@ mflag_body() atf_check -o inline:"test1:2\n" grep -m 2 -EHc "a|b|e|f" test1 } + +atf_test_case zgrep_multiple_files +zgrep_multiple_files_head() +{ + atf_set "descr" "Ensures that zgrep functions properly with multiple files" +} +zgrep_multiple_files_body() +{ + echo foo > test1 + echo foo > test2 + atf_check -o inline:"test1:foo\ntest2:foo\n" zgrep foo test1 test2 + + echo bar > test1 + atf_check -o inline:"test2:foo\n" zgrep foo test1 test2 + + echo bar > test2 + atf_check -s exit:1 zgrep foo test1 test2 +} # End FreeBSD atf_init_test_cases() @@ -944,5 +962,6 @@ atf_init_test_cases() atf_add_test_case fgrep_oflag atf_add_test_case cflag atf_add_test_case mflag + atf_add_test_case zgrep_multiple_files # End FreeBSD } diff --git a/usr.bin/grep/zgrep.sh b/usr.bin/grep/zgrep.sh index 49f2bb002634..105507f23708 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202102111521.11BFL6fq005543>