Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grep Option Error #1

Closed
earthgecko opened this issue Nov 3, 2009 · 3 comments
Closed

grep Option Error #1

earthgecko opened this issue Nov 3, 2009 · 3 comments

Comments

@earthgecko
Copy link

Just to let you know that the version of grep that I am using does not have --exclude-dir option in this version. Below is the output from running graudit.


-bash-3.2# ./graudit -d php /tmp/www.of-networks.co.uk
grep: unrecognized option `--exclude-dir=.svn'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
-bash-3.2# grep --version
grep (GNU grep) 2.5.1

Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

-bash-3.2#

Having looked through the code you may want to consider using just the grep --exclude option and passing a regex of the svn cvs and git dirs. This should achieve the same thing.
So passing grep the following:


        --exclude="*\.svn" \
        --exclude="*\.cvs" \
        --exclude="*\.git" \

Have made the modification to graudit and tested it on my version and those option parameters are working, so I will fork the file and update this issue.

@earthgecko
Copy link
Author

Here is the fork:

http://github.com/earthgecko/graudit/blob/6b38597185dbd4033b54b0e63b75141b1d6f41b1/graudit

Nice work by the way, I like it a lot already... so much so that I decided
to fix it :) So I could see it work! Very cool... and that is my first
ever open source bug fix :)

@earthgecko
Copy link
Author

As wireghoul has pointed out this grep exclude option does not work. It only works on filenames, not directory paths. I think I may have found a lovely poetic way to solve it.

Working on it now, but will take a while to test this time :)

@earthgecko
Copy link
Author

A grep version "workaround" has been found and commited to http://github.com/earthgecko/graudit/blob/2ff761c894509a9ffb7c3dfdd6a5e1731a8f202f/graudit sorry I have still not got this github sussed yet.


################################################################################
# START - Issue 1 - http://github.com/wireghoul/graudit/issues#issue/1
# grep version issue
# Modified 20091104 - Gary Wilson (gary.wilson the-at-thing of-networks the-dot-thing co the-dot-thing uk)

# Check client grep version
GREP_VERSION=`grep --version | grep "GNU grep" | sed -e "s/grep (GNU grep) //g;s/\.//g"`
if [ $GREP_VERSION -lt "253" ]; then
  grep --color=$color \
       -n -R -H -C $context -E \
       -f "$database" "$@" \
      | sed -e"s/--/$separator/"  | grep -v '/\.svn/.*[0-9][-:]\|/.cvs/.*[0-9][-:]\|/.git/.*[0-9][-:]' | sed '/#$/!b;N;/#$/d'
else
  grep --color=$color \
          --exclude-dir=.svn \
          --exclude-dir=.cvs \
          --exclude-dir=.git \
          -n -R -H -C $context -E \
          -f "$database" "$@" \
          | sed -e"s/--/$separator/"
fi

# END - Issue 1
################################################################################

This has been tested against .svn, .cvs and .git directories and works. However, for clients running an older version of grep than 2.5.3, the methodology of the main grep is changed and .svn, .cvs and .git directories ARE grepped through, but any results from those directories are then excluded via a grep -v (so do not match).

Further to this the resultant orphaned separator lines need to be cleaned up as well and all separator lines followed by another separator line are both removed.

Just a brief breakdown of the additions for older clients:

| grep -v '/\.svn/.*[0-9][-:]\|/.cvs/.*[0-9][-:]\|/.git/.*[0-9][-:]'

Piping the output through grep again to remove any lines that include a /.svn/ with anything following until a number and : or - is encountered. This should overcome any edge cases where the code has a potential security flaw and the author has also ended the line with a comment that references a /.svn/ path, extreme edge case but.. This should overcome that by having the line include the graudit output format with the line number enclosed in colons (:) or minuses (-).

| sed '/#$/!b;N;/#$/d'

Due to this methodology now essentially including .svn, .cvs and .git dirs and THEN removing them, it leaves multiple graudit separator lines in the output, for .svn, .cvs or .git lines that have been removed. Here we check if the line ends in a # and if not we skip it. If the line ends with a # and we fetch the next line and put them together. If the combined two lines match this pattern (end with a #), we delete them.

As for the grep version check it should be good until the release of version 2.6 :)

It is not the nicest or neatest workaround in the world, but... it is not the simplest problem either :) At least it would get graudit working with all grep clients straight away.

Hopefully not missed anything this time.

wireghoul pushed a commit that referenced this issue Sep 23, 2020
wireghoul pushed a commit that referenced this issue Sep 23, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant