【问题标题】:Get OS version on OS X在 OS X 上获取操作系统版本
【发布时间】:2013-01-25 23:55:47
【问题描述】:

我有一个旧脚本,用于在 Ubuntu 中运行。 现在,我有一台 Mac,想重复使用该脚本。

有谁知道 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)

谢谢

【问题讨论】:

    标签: python linux macos unix ubuntu


    【解决方案1】:

    uname -r 在 Darwin 下的工作方式相同。内核版本不是大多数人谈论或关心的东西,但它就在那里。唯一的问题是 cut 不支持 --delim 长选项,所以,试试这个吧:

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

    不过,Darwin 的内核版本控制与 Linux 的完全不同,因此在此处运行 cut 的目的尚不清楚。 (事实上​​,在 Linux 上也不是很清楚,因为版本控制方案在 3.0 版本中发生了显着变化。)

    要获取当前版本的 Mac OS(大致相当于您为 Ubuntu 获取的“发行版”),您可以使用以下命令:

    sw_vers -productVersion
    

    【讨论】:

    • 内核版本 uname -r| 的“剪切”部分出现错误cut --delim=\'.\' -f1-2 cut:非法选项 -- - 用法:cut -b list [-n] [file ...] cut -c list [file ...] cut -f列表 [-s] [-d delim] [文件 ...]
    • 哎呀,错过了。查看编辑,或完全删除 cut 部分。
    • 第二个要求我会使用sw_vers -productVersion,Apple 的人喜欢移动东西。
    • @mmgp:哦,很好的发现!我不知道sw_vers
    【解决方案2】:

    您可以使用 python“平台”模块(我无法访问 Ubuntu,请尝试发布您的发现:)

    1. 使用 platform.system() 区分 Linux 或 Darwin
    2. 调用platform.release()获取内核版本
    3. 调用 platform.linux_distribution() 或 platform.mac_ver() 以获取供应商特定的版本号。

    在 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')
    >>> 
    

    在 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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      • 2011-03-27
      • 2019-01-19
      • 1970-01-01
      相关资源
      最近更新 更多