Increase build number in C/C++ header (using Awk)

This trick works with most IDE’s that use pre-build step, but easiest I guess is to use it in a Makefile. Feel free to modify the “script” in any way it suites your purposes.

Suppose you have a build number defined as #define BuildNum 1 in BuildNum.h header.

All you have to do is add the following command in your make all section in Makefile ahead of other dependencies (it’s one line, broken here for visibility):

awk '$2 !~ /BuildNum/ {print} $2 ~ /BuildNum/ {print "#define BuildNum "$3+1}'\
    BuildNum.h > BuildNum.h~; mv BuildNum.h~ BuildNum.h

Yes, it’s a clumsy solution, but it does the work just fine… 😀 Sure, you can catch the lines in array, and afterwards print them one by one back to file, etc. … but it’s just not at all pretty bash script (enhancements and suggestions welcome!).

Notice that the command is pretty much dependent on the format of the line the BuildNum is defined on, and that “BuildNum” should not appear anywhere else as a second word on the line!

Practical Introduction to BASH

BASH, aka Bourne Again SHell, is probably the most wide-spread shell for *nix like systems (no discussions about this, please; if you prefer some other, just enjoy, I’m not making anyone use bash).

There are many intros out there, but most of them are more about syntax etc., with just a little practical use.
I wrote quite a few bash scripts, but it’s not my primary “language” of choice; so I often need a fast reminder, rather than textbook. That’s why I compiled this simple “list” or “intro” to use as a reference. Maybe someone else will find this useful also.

Continue reading