From 43bcc71d074a10d9186e27f9bf21b9dafa815bf5 Mon Sep 17 00:00:00 2001 From: kxxt Date: Wed, 21 Sep 2022 20:51:03 +0800 Subject: [PATCH 1/2] refactor: use grep -E/-F instead of fgrep/egrep --- src/etc/cat-and-grep.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/etc/cat-and-grep.sh b/src/etc/cat-and-grep.sh index 77dc52a935070..08de25a77783a 100755 --- a/src/etc/cat-and-grep.sh +++ b/src/etc/cat-and-grep.sh @@ -26,7 +26,7 @@ Options: -i Case insensitive search. ' -GREPPER=fgrep +GREPPER=grep INVERT=0 GREPFLAGS='q' while getopts ':vieh' OPTION; do @@ -39,7 +39,7 @@ while getopts ':vieh' OPTION; do GREPFLAGS="i$GREPFLAGS" ;; e) - GREPPER=egrep + GREPFLAGS="E$GREPFLAGS" ;; h) echo "$USAGE" @@ -51,6 +51,15 @@ while getopts ':vieh' OPTION; do esac done +# an utility function to check if a string contains a substring +stringContain() { [ -z "$1" ] || { [ -z "${2##*$1*}" ] && [ -n "$2" ];};} + +if ! stringContain 'E' "$GREPFLAGS" +then + # use F flag if there is not an E flag + GREPFLAGS="F$GREPFLAGS" +fi + shift $((OPTIND - 1)) # use gnu version of tool if available (for bsd) From 6135aff27cedcf81ff863715685808829364f7cb Mon Sep 17 00:00:00 2001 From: kxxt Date: Mon, 26 Sep 2022 21:56:08 +0800 Subject: [PATCH 2/2] simplify --- src/etc/cat-and-grep.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/etc/cat-and-grep.sh b/src/etc/cat-and-grep.sh index 08de25a77783a..238f7f5b66027 100755 --- a/src/etc/cat-and-grep.sh +++ b/src/etc/cat-and-grep.sh @@ -51,10 +51,7 @@ while getopts ':vieh' OPTION; do esac done -# an utility function to check if a string contains a substring -stringContain() { [ -z "$1" ] || { [ -z "${2##*$1*}" ] && [ -n "$2" ];};} - -if ! stringContain 'E' "$GREPFLAGS" +if ! echo "$GREPFLAGS" | grep -q E then # use F flag if there is not an E flag GREPFLAGS="F$GREPFLAGS"