Created
February 1, 2023 13:58
-
-
Save tst2005/7c652639d1a784c4d7ef9fa649affe68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# this is trying to mimick parts of GNU grep with busybox grep. | |
# We convert long arguments to short ones basically. | |
# this is all that busybox grep supports (with the GNU long argument version) | |
# -H --with-filename | |
# -h --no-filename | |
# -n --line-number | |
# -l | |
# -L --files-without-matches | |
# -c --count | |
# -o --only-matching | |
# -q --quiet --silent | |
# -v --invert-match | |
# -s --no-messages | |
# -r --recursive | |
# -R --dereference-recursive | |
# -i --ignore-case | |
# -w --word-regexp | |
# -x --line-regexp | |
# -F --fixed-strings | |
# -E --extended-regexp | |
# -m --max-count=NUM | |
# -A --after-context=NUM | |
# -B --before-context=NUM | |
# -C --context=NUM | |
# -e --regexp=PATTERNS | |
# -f --file=FILE | |
check_option_requires_arg() { | |
case "$1" in | |
(-[mABCef]|--max-count|--after-context|--before-context|--context|--regexp|--file) | |
false;; | |
(*) true;; | |
esac | |
} | |
replace_option() { | |
case "$1" in | |
(--with-filename) new=-H;; | |
(--no-filename) new=-h;; | |
(--line-number) new=-n;; | |
(--files-without-matches) new=-L;; | |
(--count) new=-c;; | |
(--only-matching) new=-o;; | |
(--quiet|--silent) new=-q;; | |
(--invert-match) new=-v;; | |
(--no-messages) new=-s;; | |
(--recursive) new=-r;; | |
(--dereference-recursive) new=-R;; | |
(--ignore-case) new=-i;; | |
(--word-regexp) new=-w;; | |
(--line-regexp) new=-x;; | |
(--fixed-strings) new=-F;; | |
(--extended-regexp) new=-R;; | |
(--max-count) new=-m;; | |
(--after-context) new=-A;; | |
(--before-context) new=-B;; | |
(--context) new=-C;; | |
(--regexp) new=-e;; | |
(--file) new=-f;; | |
(*) new="$1";; | |
esac | |
} | |
i=$# | |
while [ $i -gt 0 ]; do | |
case "$1" in | |
(*=*) | |
replace_option "${1%%=*}" | |
set -- "$@" "$new" "${1#*=}" | |
;; | |
(*) | |
replace_option "$1" | |
set -- "$@" "$new" | |
if ! check_option_requires_arg "$1" ;then | |
i=$(( $i -1)) | |
shift; set "$@" "$1" | |
fi | |
;; | |
esac | |
i=$(( $i -1)) | |
shift | |
done | |
echo "$# args: $*" | |
# grep "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment