2013-07-10 94 views
0

我想檢查OS X版本的工作站,如果它是10.7或更高版本,請執行此操作。另一方面,如果在10.7之前做了別的事情。你們能否指出我正確的方向,爲什麼我會在下面看到錯誤信息?幫助比較聲明

非常感謝!

10.8.4

10.8

10.8

/Users/Tuan/Desktop/IDFMac.app/Contents/Resources/:運行上述腳本時

#!/bin/sh 

cutOffOS=10.7 
osString=$(sw_vers -productVersion) 
echo $osString 
current=${osString:0:4} 
echo $current 
currentOS=$current 
echo $currentOS 
if [ $currentOS >= cutOffOS ] ; then 
    echo "10.8 or later" 
    chflags nohidden ~/Library 
else 
    echo "oh well" 
fi 

輸出腳本:第11行:[:10.8:一元運算符預計

哦好的

回答

1

忽略sw_vers可以返回包含3個部分的版本「數字」的(非常實際的)問題(例如, 10.7.5),bash不能處理浮點數,只能是整數。您需要將版本號分解爲整數部分,並分別進行測試。

cutoff_major=10 
cutoff_minor=7 
cutoff_bug=0 

osString=$(sw_vers -productVersion) 
os_major=${osString%.*} 
tmp=${osString#*.} 
os_minor=${tmp%.*} 
os_bug=${tmp#*.} 

# Make sure each is set to something other than an empty string 
: ${os_major:=0} 
: ${os_minor:=0} 
: ${os_bug:=0} 

if [ "$cutoff_major" -ge "$os_major" ] && 
    [ "$cutoff_minor" -ge "$os_minor" ] && 
    [ "$cutoff_bug" -ge "$os_bug" ]; then 
    echo "$cutoff_major.$cutoff_minor.$cutoff_bug or later" 
    chflags nohidden ~/Library 
else 
    echo "oh well" 
fi 
+0

好的!只是爲了好奇:如果一個版本是3數字格式,而另一個版本只有2個格式呢? – fedorqui

+0

好點;我忘了說明這一點。在'bash'中,你可以在'((x> y))'中使用未定義的變量,它們將被視爲0.對於POSIX shell,你必須明確地設置空變量爲0。我會更新。 – chepner