1

我有一个旧脚本,用于在 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)

谢谢

4

2 回答 2

3

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

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

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

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

sw_vers -productVersion
于 2013-02-10T21:22:52.277 回答
1

您可以使用 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')
于 2013-10-27T11:11:42.930 回答