简体   繁体   English

在OS X上获取OS版本

[英]Get OS version on OS X

I have an old script which I use to run in Ubuntu. 我有一个旧脚本,我用它在Ubuntu中运行。 Now, I have a Mac and want to reuse the script. 现在,我有一台Mac并希望重用该脚本。

Does anyone know what would be equivalent of the following commands in Mac OS? 有谁知道在Mac OS中什么等同于以下命令?

def runCmd(cmd):
    p = subprocess.Popen(cmd,
                         shell=True, 
                         stdin=subprocess.PIPE, 
                         stdout=subprocess.PIPE, 
                         stderr=subprocess.PIPE, 
                         close_fds=True)
    result=p.stdout.readlines()
    s=result[0].split()[0]
    return s

def getKernelVer():
    cmd="uname -r| cut --delim=\'.\' -f1-2"
    return runCmd(cmd)

def getUbuntuVer():
    cmd="lsb_release  -a | grep Release | cut -f 2"
    return runCmd(cmd)

Thanks 谢谢

uname -r works identically under Darwin. uname -r在达尔文下的工作方式相同。 The kernel version isn't something that most people talk or care about, but it's there. 内核版本不是大多数人谈论或关心的东西,但它就在那里。 Only gotcha is that cut doesn't support the --delim long option, so, try this instead: 只有问题是cut不支持--delim long选项,所以,试试这个:

uname -r | cut -d. -f1-2

Kernel versioning is quite different for Darwin than for Linux, though, so the purpose of running cut here is unclear. 但是,对于Darwin而言,内核版本控制与Linux完全不同,因此在此处运行cut的目的尚不清楚。 (In fact, it's not quite clear on Linux either, as the versioning scheme changed significantly with the release 3.0.) (事实上​​,在Linux上也不是很清楚,因为版本控制方案在版本3.0中发生了显着变化。)

To get the current version of Mac OS (roughly equivalent to the "release" you're getting for Ubuntu), you can use the command: 要获得当前版本的Mac OS(大致相当于您为Ubuntu获取的“版本”),您可以使用以下命令:

sw_vers -productVersion

You could use the python "platform" module (i've no access to Ubuntu, pls try and post your finding :) 您可以使用python“platform”模块(我无法访问Ubuntu,请尝试并发布您的发现:)

  1. use platform.system() to distinguish between Linux or Darwin 使用platform.system()来区分Linux或Darwin
  2. call platform.release() to get kernel version 调用platform.release()来获取内核版本
  3. call platform.linux_distribution() or platform.mac_ver() to get vendor specific version number. 调用platform.linux_distribution()或platform.mac_ver()以获取供应商特定的版本号。

On CentOS: 在CentOS上:

$ python
Python 2.7.5 (default, Jul 23 2013, 17:26:16) 
[GCC 4.7.2 20121015 (Red Hat 4.7.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.system()
'Linux'
>>> platform.release()
'2.6.32-358.18.1.el6.x86_64'
>>> platform.linux_distribution()
('CentOS', '6.4', 'Final')
>>> 

On OS X: 在OS X上:

$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.system()
'Darwin'
>>> platform.release()
'13.0.0'
>>> platform.mac_ver()
('10.9', ('', '', ''), 'x86_64')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM