Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

What's the most robust way to parse a source package's debian/changelog file (or debian/control?) to get the package's version number? Something like:

pkgver=$(dpkg-parsechangelog | magic_goes_here)

I want to extract the version number (and only the version number) so that I can manipulate it in a script.

I was expecting to find a dpkg-* utility for this purpose, but I haven't found one yet.

share|improve this question

4 Answers 4

up vote 2 down vote accepted

If you have version 1.17.0 or later, you can use

dpkg-parsechangelog --show-field Version

No need to process the output further then. This version is currently (February 2014) available in Debian Testing.

share|improve this answer
1  
@RichardHansen: Sorry about that, it turns out that the flag isn't available in the version present in Debian Stable. –  Martin Geisler Feb 21 at 7:44
    
Same for Ubuntu 12.04. It is present in Ubuntu 14.04 though. –  Stefan Jul 14 at 15:48

There are numerous ways to do this.

dpkg-parsechangelog | sed -n 's/^Version: //p'

or alternatively:

dpkg-parsechangelog | grep Version: | cut -d' ' -f2-

share|improve this answer
    
I was hoping for something a bit more robust -- the deb-control(5) man page does not clearly describe the format and I don't want to make any assumptions. For example, it does not make it clear that fields must start on a line ("Fields are delimited only by field tags."). It also says that a field can span multiple lines, and it doesn't say anything about spacing between the tag and the value, so maybe Version:123 should be interpreted as a continuation of the previous field and not a separate Version field (due to the lack of a space after the colon). –  Richard Hansen Aug 15 '13 at 16:56
    
In practice, the output from dpkg-parsechangelog is going to look like Version: 0.6.11-2 If you want something to do more robust parsing of the debian/control format, you could use python-debian or libparse-debcontrol-perl. –  andrewsomething Aug 15 '13 at 18:55

dpkg-parsechangelog works, and the earlier answer piping the output through sed/grep should be entirely robust. If you want to know precise details of the format of dpkg-parsechangelog output, and most other debian-style control files, see RFC 822. It is never ok for a deb package version to contain a newline, space, or any other special or control characters (see man deb-version), so the "Version: blah" line in the output will always be present, and it will always be a single line on its own.

However, dpkg-parsechangelog is a very heavy program to run just to get the current version number from a changelog. It has to run Perl and load an impressively large number of libraries in order to give you a result, most of which you won't use. On slower platforms, or with slow file storage media, or when you need to do this version parsing lots of times, it will prove quite painfully slow. Instead, just use whatever is inside the first set of parentheses on the first line:

head -1 debian/changelog | awk -F'[()]' '{print $2}'

That will get you the correct current package version with any valid changelog file using the standard format (and nonstandard debian/changelog formats are, for all practical general purposes, never used).

share|improve this answer

as I have seen package version consists at header part of the changelog file. so you can do it like

pkgver=$(dpkg-parsechangelog | head

I have tried this command for installed packages, for example

aptitude changelog shutter | head

&

jai@frank-Jai:~$ aptitude changelog shutter | head
Get: Changelog of shutter
shutter (0.89.0-0ubuntu1) quantal; urgency=low

  * New upstream release.
   - Adds an AppIndicator (LP: #805464).
  * debian/control: Recommend libgtk2-appindicator-perl

 -- Andrew Starr-Bochicchio <[email protected]>  Sat, 11 Aug 2012 17:36:18 -0400

shutter (0.88.3-1) unstable; urgency=low
jai@frank-Jai:~$ 
share|improve this answer
1  
I want the version number and only the version number so that I can manipulate it in a script. –  Richard Hansen Aug 15 '13 at 4:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.