Sometimes your scripts need to behave differently on different Linux's. How can I determine which version of Linux a script is running on?
-
You can find the kernel version by running
uname -a
, finding the distro version is dependant on the distro.On Ubuntu and some other OS' you can run
lsb_release -a
or read /etc/lsb_releaseDebian stores the version in /etc/debian_version
Josh Kelley : +1 for lsb_release (also works on Red Hat derivatives if the right package is installed)From Adam Gibbins -
There is no cross-distribution way. However:
- Redhat and friends: Test for /etc/redhat-release, check contents
- Debian: Test for /etc/debian_version, check contents
- Mandriva and friends: Test for /etc/version, check contents
- Slackware: Test for /etc/slackware-version, check contents
Etc. Generally speaking, check for
/etc/*-release
and/etc/*-version
.
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.
#!/bin/bash get_distribution_type() { local dtype # Assume unknown dtype="unknown" # First test against Fedora / RHEL / CentOS / generic Redhat derivative if [ -r /etc/rc.d/init.d/functions ]; then source /etc/rc.d/init.d/functions [ zz`type -t passed 2>/dev/null` == "zzfunction" ] && dtype="redhat" # Then test against SUSE (must be after Redhat, # I've seen rc.status on Ubuntu I think? TODO: Recheck that) elif [ -r /etc/rc.status ]; then source /etc/rc.status [ zz`type -t rc_reset 2>/dev/null` == "zzfunction" ] && dtype="suse" # Then test against Debian, Ubuntu and friends elif [ -r /lib/lsb/init-functions ]; then source /lib/lsb/init-functions [ zz`type -t log_begin_msg 2>/dev/null` == "zzfunction" ] && dtype="debian" # Then test against Gentoo elif [ -r /etc/init.d/functions.sh ]; then source /etc/init.d/functions.sh [ zz`type -t ebegin 2>/dev/null` == "zzfunction" ] && dtype="gentoo" # For Slackware we currently just test if /etc/slackware-version exists # and isn't empty (TODO: Find a better way :) elif [ -s /etc/slackware-version ]; then dtype="slackware" fi echo $dtype }
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:
- Pull in a known, "common init script function" type of file. Those are distribution-specific. If it doesn't exist, skip to next distribution check.
- Check the existence of a specific, known-to-exist, often-used and unlikely to be renamed function from that core script. We do that using the
type
Bash builtin.type -t
returnsfunction
if that symbol is a function. We prependzz
to the output fromtype -t 2>/dev/null
because if the name isn't defined the output string would be empty and we'd get a syntax error about a missing left hand to the==
operator. If the name we just checked isn't a function, skip to next distribution check, otherwise we found the distribution type. - Finally, echo the distribution type so the function output can be easily used in a case .. esac block.
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.:
source /path/to/this/script.sh get_distribution_type
at the bash prompt.<
gyaresu : I haven't looked at why but it returns to prompt on Debian Lenny (5.0).Mihai Limbăşan : gyaresu, have you actually invoked the get_distribution_type function? I've edited the post to clarify (see at the bottom.)Mihai Limbăşan : @gyaresu: If the above wasn't the problem, can you please try to replace log_begin_msg in the Debian section with log_warning_msg and retry? Might have gotten the function name wrong. In any case, it should have returned "unknown" if that function wasn't htere, but still.gyaresu : @Mihai Doh! Sorry. Didn't read the script properly. Was early, no coffee. My apologise. gyaresu@debian:~/bin$ source server_version.sh gyaresu@debian:~/bin$ get_distribution_type debianMihai Limbăşan : @gyaresu: Thanks! That's good, should help jldugger to know that it works on Debian as well :)From Mihai Limbăşan -
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.:
Welcome to SUSE Linux Enterprise Server 10 SP2 (i586) - Kernel \r (\l).
And yes I know it's suboptimal. :)
Brad Gilbert : It may be sub-optimal, but it is in the same place.From Node -
Most distro's have a unique method of determining the particular distribution.
For example:
Redhat (And derivatives): /etc/redhat-release SUSE: /etc/SUSE-release
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.
lsb_release -a
From Mark Turner -
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.
From womble -
The version of linux is a hard question. If we look at it narrowly we have the kernel version you can get with "
uname -r
". Distribution version is mostly irrellevant. Some distributions are better (enterprise distributions such as Redhat Enterprise Linux). Other distributions like Gentoo are basically moving targets that have no sensible version at all. If you need to do things based on version, take a look at major components that are relevant to you:Component Version command glibc /lib/libc.so.6 gcc gcc --version X xdpyinfo libX11 pkg-config --modversion x11 gtk+ pkg-config --modversion gtk+-2.0 qt-4 pkg-config --modversion QtCore etc...
From Paul de Vrieze -
facter is a handy tool for this sort of discovery, although it probably uses some of the methods detailed above, and requires Ruby.
From Andrew H -
All you need to do is type
uname -a
at your favorite shell. That will print out the kernel name and version.From David Collantes -
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
From prasanna -
You can also check the Grub menu, usually gives you a bunch of distro/version info :-)
From Antoine Benkemoun -
I've found that
cat /etc/*release*
almost always works.From ibuys -
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.
From unhillbilly
0 comments:
Post a Comment