Sometimes your scripts need to behave differently on different Linux's. How can I determine which version of Linux a script is running on?
|
|
Don't try and make assumptions based on the distro as to what you can and cannot do, for that way lies madness (see also "User Agent detection"). Instead, detect whether what it is that you want to do is supported, and how it's done by whatever command or file location you want to use. For example, if you wanted to install a package, you can detect whether you're on a Debian-like system or a RedHat-like system by checking for the existence of dpkg or rpm (check for dpkg first, because Debian machines can have the rpm command on them...). Make your decision as to what to do based on that, not just on whether it's a Debian or RedHat system. That way you'll automatically support any derivative distros that you didn't explicitly program in. Oh, and if your package requires specific dependencies, then test for those too and let the user know what they're missing. Another example is fiddling with network interfaces. Work out what to do based on whether there's an /etc/network/interfaces file or an /etc/sysconfig/network-scripts directory, and go from there. Yes, it's more work, but unless you want to remake all the mistakes that web developers have made over the past decade or more, you'll do it the smart way right from the start. |
|||
|
|
There is no cross-distribution way. However:
Etc. Generally speaking, check for Edit: Found an old (1+ years) bash script of mine lying around that I must have cobbled together over the years (it has an impressive CVS log going back 6 years.) It might not work properly anymore as-is and I can't be bothered to find installed distros to test against, but it should provide you with a good starting point. It works fine on CentOS, Fedora and Gentoo. gyaresu tested it successfully on Debian Lenny.
Note that this will probably only work correctly in Bash. You could rewrite it ofr other shells. That being said, you might want to test for features, not for distributions. I'm not using this anymore simply because it became a maintenance burden. It's easier to rely on cross-distribution tools and solutions. Conceptually, what it does is, in order:
Edit in case you're trying to run this as a straight script: This script is supposed to get sourced or included from other scripts. It does not output anything on its own if you run it as-is. To test it, source it and then invoke the function, e.g.:
at the bash prompt.< |
|||||||||||
|
|
You can find the kernel version by running On Ubuntu and some other OS' you can run Debian stores the version in /etc/debian_version |
|||
|
|
Most distro's have a unique method of determining the particular distribution. For example:
There is a standard out there known as the Linux Standard Base or LSB. It defines that there should be a file called /etc/lsb-release or a program called lsb_release that will echo back information about your linux distro.
|
|||
|
|
|
In addition to the other answers: If you just want to parse one file, most distros personalize the tty login via /etc/issue e.g.:
And yes I know it's suboptimal. :) |
|||
|
|
|
facter is a handy tool for this sort of discovery, although it probably uses some of the methods detailed above, and requires Ruby. |
|||
|
|
|
All you need to do is type |
|||
|
|
|
I concur with Mark, Adam and Mihai (can't vote up due to insufficient reputation). Solutions based on LSB and its relative FHS will work with most distributions and are likely to continue working in future. LSB and FHS are your friends. |
|||
|
|
|
||||
|
|
The version of linux is a hard question. If we look at it narrowly we have the kernel version you can get with "
|
|||
|
|
|
You can also get the version by cat /proc/version o/p: Linux version 2.6.17-13mdv (rtp@octopus.mandriva.com) (gcc version 4.1.2 20070302 (prerelease) (4.1.2-1mdv2007.1)) #1 SMP Fri Mar 23 19:03:31 UTC 2007 |
|||
|
|
|
You can also check the Grub menu, usually gives you a bunch of distro/version info :-) |
|||
|
|
|
FusionInventory is a cross-platform lightweight inventory tool that can get this information on many Linux distros but also on BSDs, Windows, MacOS X and other unices. If available, they use I would recommend using FusionInventory itself to get this information, rather than reimplementing your own scripts with this logic, as their community will maintain this functionality up to date. You could either use the agent on it's own (it outputs a XML/JSON file which is easy to parse) or couple it with a wider solution to manage the machines in your network like GLPI or Rudder, depending on your needs. |
|||
|
|