title: Git pre-commit hook to find text in files
body: <p>I'm writing a git pre-commit hook to check if any of the staged files are containing disallowed text and abort if that's the case.</p>
  
  <p>Not an expert at this. So far I've got this</p>
  
  <pre><code>git diff --cached --name-status | while read x file; do
          if [ "$x" == 'D' ]; then continue; fi
          if [[ egrep "DISALLOWED_TEXT" ${file}]]; then
                  echo "ERROR: Disallowed text in file: ${file}"
                  exit 1
          fi
  done
  </code></pre>
  
  <p>Doesn't seem to work. I'm getting these errors while commiting:</p>
  
  <pre><code>.git/hooks/pre-commit: line 16: conditional binary operator expected
  .git/hooks/pre-commit: line 16: syntax error near `"DISALLOWED_TEXT"'
  .git/hooks/pre-commit: line 16: `        if [[ egrep "DISALLOWED_TEXT" ${file}]]; then'
  </code></pre>
  
  <p>Any suggestions, ideas and help appreciated.
  Thanks!</p>
  
  <p><strong>SOLVED:</strong> (syntax errors and dysfunctional exit call)</p>
  
  <pre><code>disallowed="word1 word2"
  
  git diff --cached --name-status | while read x file; do
          if [ "$x" == 'D' ]; then continue; fi
          for word in $disallowed
          do
              if egrep $word $file ; then
                  echo "ERROR: Disallowed expression \"${word}\" in file: ${file}"
                  exit 1
              fi
          done
  done || exit $?
  </code></pre>