0

我正在尝试检查工作站的 OS X 版本,如果它是 10.7 或更高版本,请执行此操作。另一方面,如果是在 10.7 之前,请执行其他操作。你们能否指出我为什么会收到以下错误消息的正确方向?

非常感谢!

#!/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

运行上述脚本时的输出:

10.8.4

10.8

10.8

/Users/Tuan/Desktop/IDFMac.app/Contents/Resources/script:第11行:[:10.8:预期一元运算符

那好吧

4

1 回答 1

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
于 2013-07-10T13:43:30.587 回答