?
Solved

shell script to extract version numbers

Posted on 2003-03-25
8
Medium Priority
?
478 Views
Last Modified: 2013-12-06
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
Comment
Question by:zebada
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 5

Accepted Solution

by:
ecw earned 800 total points
ID: 8206472
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
 
LVL 6

Author Comment

by:zebada
ID: 8206645
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
 

Expert Comment

by:hkabla
ID: 8239572
An even shorter solution:

grep "@(#)" version.c 2>/dev/null | awk -Fprog '{print $3}' | awk '{print $1"."$3}'
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 1

Expert Comment

by:Santunes
ID: 8245724
An even more shorter solution:

awk '/@\(#)/ {print $4"."$6}' version.c
0
 
LVL 6

Author Comment

by:zebada
ID: 8273115
I haven't forgotten this Q. I am on leave, I'll test it when I get back next week :)
Paul
0
 

Expert Comment

by:carlls
ID: 8276303
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
 
LVL 6

Author Comment

by:zebada
ID: 8277021
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
 
LVL 6

Author Comment

by:zebada
ID: 8296922
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question