【问题标题】:rhel os version detecting in makefile在 makefile 中检测 rhel os 版本
【发布时间】:2014-10-01 12:13:24
【问题描述】:

我有 2 个系统,一个是 rhel4,另一个是 rhel6。 在编译时我想区分并进行相应的编译。

我的方法:

在 rhel5 机器上

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

在 rhel4 机器上

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

所以我想用这样的东西-

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

有没有更好的方法来做到这一点? 任何默认包含此信息的标志,我可以使用它吗?

【问题讨论】:

  • 你的代码能用吗?
  • 这只是我的想法。
  • ifeq ($(shell cat /etc/redhat-release | cut -d "." -f 1 | cut -d " " -f 7),6) CFLAGS += -DRhel6=1否则 CFLAGS += -DRhel6=0 endif

标签: c unix makefile


【解决方案1】:

在 Makefiles 中,我更喜欢可读性,而不是性能或复杂的代码。你的代码版本看起来很简单。只是你的版本中的多个剪切命令,可以简化为一个 awk,如下所示。

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

此外,我还要指出,/etc/redhat-release 文件是操作系统版本的可靠来源。但确实存在其他类似的命令:

$ 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)

【讨论】:

  • awk 脚本可能需要稍微复杂一些。我的/etc/redhat-release 持有CentOS Linux release 7.3.1611 (Core) 并且awk 脚本返回(Core) 而不是7 如所希望的那样。查看各种分析器:stackoverflow.com/questions/1133495/…
猜你喜欢
  • 2012-09-09
  • 2013-05-15
  • 2011-07-08
  • 2010-12-23
  • 2010-11-22
  • 2020-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多