Unix通过字符截取判断个OS版本

时间:2023-01-29 22:06:36

背景

shell脚本里,可通过这些命令进行版本的判断,对不同版本作不同的操作。

RHEL

[root@rhe Linux]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.9 (Santiago)
[root@rhel Linux]# cat /etc/redhat-release | sed -e "s/.*release //"
6.9 (Santiago)
[root@rhel Linux]# cat /etc/redhat-release | sed -e "s/.*release //" -e "s/ .*//g"
6.9
[root@rhel Linux]# cat /etc/redhat-release | sed -e "s/.*release //" -e "s/ .*//g" -e "s/\..*//g"
6

SUSE

linux-gtu2:~ # cat /etc/SuSE-release
SUSE Linux Enterprise Server 12 (x86_64)
VERSION = 12
PATCHLEVEL = 1
# This file is deprecated and will be removed in a future service pack or release.
# Please check /etc/os-release for details about this release.
linux-gtu2:~ # cat /etc/SuSE-release | grep "VERSION = "
VERSION = 12
linux-gtu2:~ # cat /etc/SuSE-release | grep "VERSION = " | sed -n 's/VERSION = //p'
12
linux-gtu2:~ #

Solaris

root@solaris114:~# cat /etc/release
                            Oracle Solaris 11.4 SPARC
  Copyright (c) 1983, 2018, Oracle and/or its affiliates.  All rights reserved.
                             Assembled 28 March 2018
root@solaris114:~# cat /etc/release | grep "Solaris"
                            Oracle Solaris 11.4 SPARC
root@solaris114:~# cat /etc/release | grep "Solaris" | awk '{print $3}'
11.4
root@solaris114:~# cat /etc/release | grep "Solaris" | awk '{print $3}' | sed -e "s/\..*//g"
11
root@solaris114:~#