shell script to extract version numbers
Posted on 2003-03-25
I need to be able to extract the version and revision numbers from a C source file as follows:
The C source file will always contain the following strictly formatted version string:
char prog_version = "@(#)prog 1 rev 02 $Date: 2003/02/25 16:25:00$ "
The final executable that is built must include the version.revision number appended to it like this:
I am using the following script to extract the version.revision number:
VSTR=`grep "@(#)" version.c 2>/dev/null`
if [ $? -eq 0 ]
POS=`expr index "$VSTR" "@"`
POS=`expr $POS + 14`
VER=`expr substr "$VSTR" $POS 3`
POS=`expr $POS + 8`
REV=`expr substr "$VSTR" $POS 2`
This works fine on Linux, HP-UX and SCO producing the correct output of: 1.02
but it produces the following incorrect output on Tru64: 1.2
Anyone know how to fix this? Or a better way to do what I want?
I need a solution that will run on all (popular) shells as this is part of our porting automation and I don't want to have to port our porting scripts - if you know what I mean.