0

I have a simple script to search patterns in my code sources, named prgrep

#!/usr/bin/bash
grep -irnI --exclude-dir={.git,obj} --exclude=tags --color=auto "$@"

(The fact that it is a script and not an alias or function is that I want to be able to call it from inside vim and with any shell)

Note that the search is case insensitive (since I consider this a good default to search) and the script accepts any flags that grep accepts.

I would like grep to have a flag --no-ignore-case so that the caller of the script could override the -i flag of the script, but GNU grep does not provide this.

Do you have any simple idea to provide such functionality? Currently I have a separate script named Prgrep which performs case sensitive searches.

EDIT
Recent versions of GNU grep do provide a --no-ignore-case option, which is exactly what I need. I'm using GNU grep 3.1, which still doesn't have this option.

7
  • How about an argument for the script? Inside the script you check $1 for any string you like and create a case statement with or without case-sensitive search. Commented Jan 28, 2021 at 9:08
  • The desired behaviour is achieved simply by dropping -i - I don't understand what the question is. Commented Jan 28, 2021 at 9:16
  • @eblock That would force a predefined number of arguments, and the script would have to handle all of them. I want to keep allowing all the grep options Commented Jan 28, 2021 at 9:20
  • 2
    I’m voting to close this question because the issue was fixed by updating grep. Commented Jan 28, 2021 at 9:35
  • 1
    @Panki OK, but it would be nice to have a solution for old versions of grep... Commented Jan 28, 2021 at 9:36

1 Answer 1

2

New versions of grep have the option --no-ignore-case which overrides -i:

--no-ignore-case
Do not ignore case distinctions in patterns and input data. This is the default. This option is useful for passing to shell scripts that already use -i, to cancel its effects because the two options override each other.

For older versions of grep, you could simply add this as option to your script:

#!/usr/bin/bash
if [ "$1" = "--no-ignore-case" ]; then
    shift
    grep -rnI --exclude-dir={.git,obj} --exclude=tags --color=auto "$@"
else
    grep -irnI --exclude-dir={.git,obj} --exclude=tags --color=auto "$@"
fi

Note: --no-ignore-case will need to be the first argument when you call your script.

2
  • The only problem with this solution is that --no-ignore-case must always be the first argument in order to be matched Commented Jan 28, 2021 at 19:55
  • @Hobber short of re-doing the entire argument parsing of grep, there's no way to robustly handle that. Consider a command line like your_wrapper -hio --no-ignore-case ...: the --no-ignore-case should override not only the -i default from your wrapper, but also the -i from the command line, which means that it will have to parse and rebuild the command line of grep; have a look at the /usr/bin/zgrep wrapper script on Linux to have a taste of what that entails ;-) Commented Jan 28, 2021 at 20:32

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.