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?
views:
45answers:
2
+1
Q:
Grep 'hg incoming' result for specific files and save result - if there is files im looking for
+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
2010-07-12 11:34:42
I know. But i didn't have any local modifications here. And i can live with wrong detection of keyword.Thanks!
NilColor
2010-07-12 14:33:52
+1
A:
count=`hg incoming -v | grep -c 'models\.py'`
if test $count -gt 0; then
./manage.py resetdb
fi
Aleksey Otrubennikov
2010-07-12 11:46:27
Use grep -c or --count instead if a count is desired. It doesn't appear to be, however.
Roger Pate
2010-07-12 11:55:21
thanks Roger, I have never seen "-c" option before, very handy one
Aleksey Otrubennikov
2010-07-12 12:50:44
@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
2010-07-13 01:27:08
@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
2010-07-13 07:30:15