0

I have 2 system one is rhel4 and other is rhel6. While compiling I want to distinguish and compile accordingly.

My approach :

in rhel5 machine

-bash-4.1$ cat /etc/redhat-release
 Red Hat Enterprise Linux Server release 6.5

in rhel4 machine

-bash-3.00$ cat /etc/redhat-release 
Red Hat Enterprise Linux AS release 4

So I am thinking to use something like this-

OSVERSION = cat /etc/redhat-release | cut -d "." -f 1 | cut -d " " -f 7
ifeq(OSVERSION,4)
   XXXXXXXXXXX
else
   YYYYYYYYYYY
endif

Is there any better way to do this? Any flag which contains this information by default and I can use it?

hari shankar
  • 27
  • 1
  • 7

1 Answers1

0

In Makefiles, I prefer readability first, than performance or complex codes. Your version of code seems simple and fine. Just that multiple cut commands in your version, could be reduced into a single awk, as below.

OSVERSION = cat /etc/redhat-release | awk 'NF>1{print $NF}'
ifeq(OSVERSION,4)
   XXXXXXXXXXX
else
   YYYYYYYYYYY
endif

Besides, I would also like to point out that, /etc/redhat-release file is a reliable source of OS versions. But other commands do exist that do the similar:

$ uname -a
Linux local.net 3.5.3-1.fc17.i686 #1 SMP Wed Aug 22 18:25:58 UTC 2013 i686 i686 i386
GNU/Linux

$ cat /etc/issue
Red Hat Enterprise Linux Server release 5.5 (Tikanga)

$ /usr/bin/lsb_release --d
Description:    Red Hat Enterprise Linux Server release 5.5 (Tikanga)
askmish
  • 6,464
  • 23
  • 42
  • 1
    The awk script may need to be a little more complex. My `/etc/redhat-release` holds `CentOS Linux release 7.3.1611 (Core)` and the awk script returns `(Core)` instead of `7` as hoped for. See the various ansers at: https://stackoverflow.com/questions/1133495/how-do-i-find-which-rpm-package-supplies-a-file-im-looking-for – Jesse Chisholm Jul 12 '17 at 18:58