views:

45

answers:

2

Hi! I want to create a script that will do something if hg incoming -v contains specific file. Say if hg incoming -v | grep models.py > 0 than do ./manage.py resetdb. Something like this.
How can i set a flag (in bash script) based on hg incoming -v | grep manage.py result?

+1  A: 
if hg incoming -v | grep -q 'models\.py'; then
  ./manage.py resetdb
fi

However, this seems fragile: it matches when models.py is present in other output than files (such as description) and won't work when you modify models.py locally. You can set a variable within the above to control later actions, if that's what you meant by "set a flag."

Roger Pate
I know. But i didn't have any local modifications here. And i can live with wrong detection of keyword.Thanks!
NilColor
+1  A: 
count=`hg incoming -v | grep -c 'models\.py'`
if test $count -gt 0; then
   ./manage.py resetdb
fi
Aleksey Otrubennikov
Use grep -c or --count instead if a count is desired. It doesn't appear to be, however.
Roger Pate
thanks Roger, I have never seen "-c" option before, very handy one
Aleksey Otrubennikov
Thanks for backticks idea. Very handy.
NilColor
@NilColor: `$()` has the same purpose yet nests better than backticks. I don't have links handy but do see some general bash scripting guides.
Roger Pate
@NilColor: check "The Unix Programming Environment" (http://www.amazon.com/Unix-Programming-Environment-Prentice-Hall-Software/dp/013937681X) for many other Unix practices
Aleksey Otrubennikov