user2568589
user2568589

Reputation: 1

Helping with comparison statement

I am trying to check the OS X version of a workstation and if it's 10.7 or higher do this. On the other hand, if it's before 10.7 do something else. Can you guys point me in the right direction as to why I am getting the error message below?

Thank you very much!

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

Output when run the above script:

10.8.4

10.8

10.8

/Users/Tuan/Desktop/IDFMac.app/Contents/Resources/script: line 11: [: 10.8: unary operator expected

oh well

Upvotes: 0

Views: 47

Answers (1)

chepner
chepner

Reputation: 531055

Ignoring the (very real) issue that sw_vers can return a version "number" with 3 parts (e.g. 10.7.5), bash cannot deal with floating point numbers, only integers. You need to break the version number into its integer components, and test them separately.

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

Upvotes: 1

Related Questions