Tuesday, March 11, 2008

Distro-specific code: lsb_release

I like to write portable code. Sometimes, I can't. lsb_release helps me solve this problem.

To find out what distro I'm on I've always written ad-hoc code that looked for some distribution-specific feature, like /etc/redhat-release and, if necessary, parsed the output.

Now, the Linux Standards Base has a command that'll do the job for me.
$ lsb_release -a 2>/dev/null
Distributor ID: Ubuntu
Description: Ubuntu 7.10
Release: 7.10
Codename: gutsy
(There's an error message saying "No LSB modules are available." on this distro, which I'm discarding.)

This is roughly analogous to "uname -a", but about the distro, not the box.
$ uname -a
Linux jsh-laptop 2.6.22-14-generic #1 SMP Tue Feb 12 07:42:25 UTC 2008 i686 GNU/Linux
It lets me sequester distro-specific actions in code like this:
case $(lsb_release -i 2>/dev/null) in
*Ubuntu*) echo This is Ubuntu ;;
*) echo This is NOT Ubuntu ;;
esac
Old pal, Nick Stoughton, our Usenix Standards Rep, points me at the relevant standard, here.

No comments: