Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
does lsb_release work? (-r ought to give you just the number; try also -a to see everything available) If so, that's also cross-vendor. –  derobert Nov 29 '12 at 19:55
    
Test for features, not platforms. Your platform test today will break when RHEL7 comes out, and may break before that when RHEL 6.next comes out, perhaps because they forward ported an EL5 feature to 6.x to silence complaints about it being removed. So, if your script needs a git Bash 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:29
    
@WarrenYoung - Thanks, but I'm not a sysasmin here, just a user of a centrally managed RHEL deployment. All of our RHEL5 machines are essentially the same image, ditto all RHEL6 machines, so for me knowing the major revision is sufficient. Given that we are only in the planning stages of moving to RHEL6 now, I think it will be a while before we have to worry about RHEL7. *8') –  Mark Booth Nov 30 '12 at 9:53
    
@derobert - No, on RHEL5 & 6 lsb_release -r includes other text too, e.g. Release: 5.8. As Dennis Kaarsemaker explains though, if you use lsb_release -rs instead, you get just the number. –  Mark Booth Nov 30 '12 at 9:57
add comment

5 Answers

up vote 2 down vote accepted

You can also use the lsb_release command. If you're already certain it's RHEL, getting the major version number is:

majversion=$(lsb_release -rs | cut -f1 -d.)
share|improve this answer
add comment
if grep -q -i "release 6" /etc/redhat-release
then
  echo "running RHEL 6.x"
fi

This would be the simplest way I can think of.

share|improve this answer
add comment

You could expand on this for multiple Linux distros by using the /etc/issue file instead of /etc/redhat-release file.

share|improve this answer
add comment

I ended up using jordanms answer, but adding a twist of my own.

Because I didn't want to have all of my rhel6 commands together and all of my rhel5 commands together, but interleaved within their own section, and I didn't want to have to replicate this code every time I wated to switch, instead I did this:

if   grep -q -i "release 6" /etc/redhat-release ; then
  rhel6only=1
elif grep -q -i "release 5" /etc/redhat-release ; then
  rhel5only=1
else
  echo "Running neither RHEL6.x nor RHEL 5.x !"
fi

That way, I could do things like:

# JDK
[ $rhel5only ] && module load java/6
[ $rhel6only ] && module load java/7

# Eclipse
module load eclipse

# Python
[ $rhel5only ] && module load python/2
[ $rhel6only ] && module load python/3

etc.

share|improve this answer
add comment

if your further intention is to manage modulefiles on per context-basis, I suggest you check out EasyBuild and some related logic, living in Python format:

share|improve this answer
add comment

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.