awk variables empty when script executed by a program - works when manually executed at command line. Why?

I have a little script that reads my git log and outputs the number of lines added and removed for the past week. When I execute this at the command line it works perfectly (including when I put this in a script and run it manually), for some reason when I put the code in a git hook it runs, but it always tell me 0 lines have been added and removed, the same is true of running the script via GeekTool.

git --git-dir=/Users/home/Thesis/.git/ log --since="1 week ago" --numstat --pretty="short" | \
awk 'NF==3 {plus+=$1; minus+=$2} END \
{printf("Added %d lines\nRemoved %d lines\n", plus, minus)}' > ~/bin/GeekTool/gitThesisChangeInLastWeek.txt

I have done some reading and googeling on this, but I'm not sure where the problem is so I'm not sure what to search for. Can anyone here help me out? It's really bugging me, since I know it's going to be something really simple! Thanks.

Posted Jun 15, 2010, 10:24 AM UTC

24
dagbrown•

Are you on a Mac? You might have two different versions of awk installed--one the original BSD awk and the other GNU awk. Try typing "which awk" and putting the full path it says awk is at into your git hook.

1
rydoOP•

I am on a mac.

I did what you said, put in the full path to awk, same behaviour unfortunately.

1
rydoOP•

After much consternation, I changed the printf line to:

{printf("Added "plus" lines\nRemoved "minus" lines\n")}

I know this is not how printf is supposed to be used, but it's working and I trust the input. I should have exhausted all possibilities myself first, clearly I didn't this time.

1
[deleted]•

Best practices and AWK are probably mutually exclusive anyway. :) Still, I love it.

2