I work in an environment where we are slowly transitioning machines from Red Hat Enterprise Linux 5 to RHEL 6.
I need some of my scripts to do something subtly different on RHEL6 machines to what is currently being done on RHEL5 machines.
The pragmatic solution is to check at runtime and run some commands on RHEL5, others on RHEL6 and some on both.
A practical example of this is that we are using environment modules and my .bashrc includes a module load git line, but on RHEL6 machines this command errors:
RHEL6 system, git should be installed - not loading module
Looking in the modulefile I find the following code:
set redhatrelease [eval exec "cat /etc/redhat-release"]
if { [regexp -nocase {release 6} $redhatrelease] } {
puts stderr "\n\t RHEL6 system, git should be installed - not loading module\n"
} else {
...
}
This seems to do what I want, but I was hoping for something shorter.
So, what is the easiest way to tell RHEL5 from RHEL6 in a bash script?
Ideally it should be robust across different major versions, but be tolerant of variations in minor release numbers.



lsb_releasework? (-rought to give you just the number; try also-ato see everything available) If so, that's also cross-vendor. – derobert Nov 29 '12 at 19:55gitBash module, you check whether it exists before calling it. When/if it appears later, your check's results change, so suddenly the feature starts working, without any extra effort. – Warren Young Nov 29 '12 at 21:29lsb_release -rincludes other text too, e.g.Release: 5.8. As Dennis Kaarsemaker explains though, if you uselsb_release -rsinstead, you get just the number. – Mark Booth Nov 30 '12 at 9:57