2013-02-10 54 views
1

我有一個用於在Ubuntu中運行的舊腳本。 現在,我有一臺Mac,並希望重新使用該腳本。在OS X上獲取操作系統版本

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

感謝

回答

3

uname -r同樣工作達爾文下。內核版本不是大多數人談論或關心的東西,但它在那裏。只有小問題,就是cut不支持--delim長選項,那麼,試試這個來代替:

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

內核版本是達爾文比Linux的完全不同,雖然如此,這裏運行cut的目的還不清楚。 (事實上​​,在Linux上也不是很清楚,因爲隨着版本3.0,版本控制方案發生了顯着變化。)

要獲得當前版本的Mac OS(大致相當於Ubuntu獲得的「發行版」 ),你可以使用命令:

sw_vers -productVersion 
+0

我上 「切」 的內核版本 使用uname -r部分得到一個錯誤|剪切--delim = \'。''-f1-2 剪切:非法選項 - - 用法:剪切-b列表[-n] [文件...] 剪切-c列表[文件...] cut -f list [-s] [-d delim] [file ...] – Fraz 2013-02-10 21:25:13

+0

哎呀,錯過了。請參閱編輯,或者完全刪除「剪切」部分。 – duskwuff 2013-02-10 21:30:09

+2

我會使用'sw_vers -productVersion'作爲第二個要求,蘋果公司的人喜歡移動。 – mmgp 2013-02-10 21:33:55

1

你可以使用Python「平臺」模塊(我已經到Ubuntu進不去,請嘗試併發布您發現:)

  1. 使用的平臺。系統()區分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')