Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

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
0
zebada
Asked:
zebada
1 Solution
 
ecwCommented:
sed -n '/@(#)/ {
  s/^.*@(#)[^  ]*[  ][  ]*//
  s/ rev /./
  s/[  ].*$//
  p
  q
  }'

** A space and a tab should be in the [] in the first and third s/ line.
Ie. -n sed will not print unless told otherwise.
    if a line contains @(#)
    Remove everything from the start of line, followed by @(#), followed by a string of non blanks, followed by 1 or more blank, eg. ending up with
1 rev 02 $Date: 2003/02/25 16:25:00$ "
    Now change / rev / to a . to give
1.02 $Date: 2003/02/25 16:25:00$ "
    Next trim off everything from the first blank to the end of the line, giving
1.02
    Print it,
    And quit.

0
 
zebadaAuthor Commented:
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
0
 
hkablaCommented:
An even shorter solution:

grep "@(#)" version.c 2>/dev/null | awk -Fprog '{print $3}' | awk '{print $1"."$3}'
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SantunesCommented:
An even more shorter solution:

awk '/@\(#)/ {print $4"."$6}' version.c
0
 
zebadaAuthor Commented:
I haven't forgotten this Q. I am on leave, I'll test it when I get back next week :)
Paul
0
 
carllsCommented:
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
0
 
zebadaAuthor Commented:
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.
0
 
zebadaAuthor Commented:
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.


0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now