Tell me more ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Trying to write a script that searches for the version of the Application then returns the value. My problem is the value is three to four intergers long (example 4.3.2).

I have searched for a while and can't find any syntax that would allow you to use a != or -ge for anything higher than a number with periods in it. Just wondering if anyone has a better way or I will just keep adding for every version release.

What I want

else if [ $version1 -ge "9.0.8" ]; then

How it is written now

vercheck=`mdls -name kMDItemVersion /Applications/iMovie.app`
version=`echo ${vercheck:17}`
version1=`echo ${version:1:5}`

[...]

else if [ $version1 = "9.0.8" ]; [ $version1 = "9.1.1" ]; then
    echo "You already have this version or a higher version installed"
    exit 0
share|improve this question
1  
Perfect thanks for the link – balooga1 Mar 1 at 17:39

1 Answer

Actually, comparing version numbers is pretty straightforward (at least as long as they are strictly numeric) as they are hierarchically structured left to right. A sequential comparison in that same order will yield a clear result.

The following bash function will return 0 (true) if two version numbers are not equal, 1 (false) if they are, as long as the variables $version_1 and $version_2 both contain only an arbitrary number of digit groups separated by periods:

function versions_not_equal {
    while [[ $version_1 != "0" || $version_2 != "0" ]]; do
        (( ${version_1%%.*} != ${version_2%%.*} )) && return 0
        [[ ${version_1} =~ "." ]] && version_1="${version_1#*.}" || version_1=0
        [[ ${version_2} =~ "." ]] && version_2="${version_2#*.}" || version_2=0
    done
    false
}

Implementing other comparisons, like greater or equal, is as simple as changing the comparison operator of the arithmetic evaluation (i.e. (( ${version_1%%.*} >= "${version_2%%.*}" ))).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.