1
0
mirror of https://github.com/facebook/zstd.git synced 2025-08-01 09:47:01 +03:00

Fix #1428 - zstdgrep now returns 1 on unmatch

This commit is contained in:
Lzu Tao
2018-11-29 02:45:01 +07:00
parent 74f8a5c51d
commit c046e0b626

View File

@ -31,94 +31,97 @@ grep_args=""
hyphen=0 hyphen=0
silent=0 silent=0
prg=$(basename $0) prg=$(basename "$0")
# handle being called 'zegrep' or 'zfgrep' # handle being called 'zegrep' or 'zfgrep'
case ${prg} in case "${prg}" in
*zegrep) *zegrep) grep_args="-E";;
grep_args="-E";; *zfgrep) grep_args="-F";;
*zfgrep)
grep_args="-F";;
esac esac
# skip all options and pass them on to grep taking care of options # skip all options and pass them on to grep taking care of options
# with arguments, and if -e was supplied # with arguments, and if -e was supplied
while [ $# -gt 0 -a ${endofopts} -eq 0 ] while [ "$#" -gt 0 ] && [ "${endofopts}" -eq 0 ]; do
do case "$1" in
case $1 in
# from GNU grep-2.5.1 -- keep in sync! # from GNU grep-2.5.1 -- keep in sync!
-[ABCDXdefm]) -[ABCDXdefm])
if [ $# -lt 2 ] if [ "$#" -lt 2 ]; then
then printf '%s: missing argument for %s flag\n' "${prg}" "$1" >&2
echo "${prg}: missing argument for $1 flag" >&2 exit 1
exit 1 fi
fi case "$1" in
case $1 in -e)
-e) pattern="$2"
pattern="$2" pattern_found=1
pattern_found=1 shift 2
shift 2 break
break ;;
;; *)
*) ;;
;; esac
esac grep_args="${grep_args} $1 $2"
grep_args="${grep_args} $1 $2" shift 2
shift 2 ;;
;; --)
--) shift
shift endofopts=1
endofopts=1 ;;
;; -)
-) hyphen=1
hyphen=1 shift
shift ;;
;; -h)
-h) silent=1
silent=1 shift
shift ;;
;; -*)
-*) grep_args="${grep_args} $1"
grep_args="${grep_args} $1" shift
shift ;;
;; *)
*) # pattern to grep for
# pattern to grep for endofopts=1
endofopts=1 ;;
;;
esac esac
done done
# if no -e option was found, take next argument as grep-pattern # if no -e option was found, take next argument as grep-pattern
if [ ${pattern_found} -lt 1 ] if [ "${pattern_found}" -lt 1 ]; then
then if [ "$#" -ge 1 ]; then
if [ $# -ge 1 ]; then pattern="$1"
pattern="$1" shift
shift elif [ "${hyphen}" -gt 0 ]; then
elif [ ${hyphen} -gt 0 ]; then pattern="-"
pattern="-"
else else
echo "${prg}: missing pattern" >&2 printf '%s: missing pattern\n' "${prg}" >&2
exit 1 exit 1
fi fi
fi fi
EXIT_CODE=0
# call grep ... # call grep ...
if [ $# -lt 1 ] if [ "$#" -lt 1 ]; then
then
# ... on stdin # ... on stdin
${zcat} -fq - | ${grep} ${grep_args} -- "${pattern}" - # shellcheck disable=SC2086
"${zcat}" -fq - | "${grep}" ${grep_args} -- "${pattern}" -
EXIT_CODE=$?
else else
# ... on all files given on the command line # ... on all files given on the command line
if [ ${silent} -lt 1 -a $# -gt 1 ]; then if [ "${silent}" -lt 1 ] && [ "$#" -gt 1 ]; then
grep_args="-H ${grep_args}" grep_args="-H ${grep_args}"
fi fi
while [ $# -gt 0 ] CUR_EXIT_CODE=0
do EXIT_CODE=1
${zcat} -fq -- "$1" | ${grep} --label="${1}" ${grep_args} -- "${pattern}" - while [ "$#" -gt 0 ]; do
shift # shellcheck disable=SC2086
"${zcat}" -fq -- "$1" | "${grep}" --label="${1}" ${grep_args} -- "${pattern}" -
CUR_EXIT_CODE=$?
if [ "${CUR_EXIT_CODE}" -eq 0 ] && [ "${EXIT_CODE}" -ne 1 ]; then
EXIT_CODE=0
fi
shift
done done
fi fi
exit 0 exit "${EXIT_CODE}"