Link to home
Start Free TrialLog in
Avatar of zebada
zebada

asked on

shell script to extract version numbers

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:
prog_1.02

I am using the following script to extract the version.revision number:

VSTR=`grep "@(#)" version.c 2>/dev/null`
if [ $? -eq 0 ]
then
  POS=`expr index "$VSTR" "@"`
  POS=`expr $POS + 14`
  VER=`expr substr "$VSTR" $POS 3`
  POS=`expr $POS + 8`
  REV=`expr substr "$VSTR" $POS 2`
  echo $VER.$REV
fi

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.

Regards
Paul
ASKER CERTIFIED SOLUTION
Avatar of ecw
ecw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zebada
zebada

ASKER

Of course, sed - I never thought of it :(
Looks good - I can't get to our Tru64 box at the moment looks like our link is down. I will test it soon.
Paul
An even shorter solution:

grep "@(#)" version.c 2>/dev/null | awk -Fprog '{print $3}' | awk '{print $1"."$3}'
An even more shorter solution:

awk '/@\(#)/ {print $4"."$6}' version.c
Avatar of zebada

ASKER

I haven't forgotten this Q. I am on leave, I'll test it when I get back next week :)
Paul
What source code control system are you using?

If you use SCCS system then the "what" command works.
If using rcs/cvs then try the "ident" command.

I use both extensively to get a "bill of materials" list on executables that report what versions of library routines and "mains" are called.

Carl
Avatar of zebada

ASKER

We were using cvs (hence the version sting) but we are transitioning to Rational, so I need a temporary way to do it until the Rational system is in place.
Avatar of zebada

ASKER

Thanks ecw - works perfectly

hkabla:
I can see what you meant with your comment but the version.c file contains the actual name of the program not "prog" literally.

Santunes:
Once I changed your comment to
awk '/@\(#)/ {print $5"."$7}' version.c
it worked perfectly - nice solution.

Thanks all, for the comments but as ecw was first and correct, the points go there.