1# 2# Test whether $ZSH_VERSION (or some value of your choice, if a second argument 3# is provided) is greater than or equal to x.y.z-r (in argument one). In fact, 4# it'll accept any dot/dash-separated string of numbers as its second argument 5# and compare it to the dot/dash-separated first argument. Leading non-number 6# parts of a segment (such as the "zefram" in 3.1.2-zefram4) are not considered 7# when the comparison is done; only the numbers matter. Any left-out segments 8# in the first argument that are present in the version string compared are 9# considered as zeroes, eg 3 == 3.0 == 3.0.0 == 3.0.0.0 and so on. 10# 11# Usage examples: 12# is-at-least 3.1.6-15 && setopt NO_GLOBAL_RCS 13# is-at-least 3.1.0 && setopt HIST_REDUCE_BLANKS 14# is-at-least 586 $MACHTYPE && echo 'You could be running Mandrake!' 15# is-at-least $ZSH_VERSION || print 'Something fishy here.' 16# 17# Note that segments that contain no digits at all are ignored, and leading 18# text is discarded if trailing digits follow, because this was the meaning 19# of certain zsh version strings in the early 2000s. Other segments that 20# begin with digits are compared using NUMERIC_GLOB_SORT semantics, and any 21# other segments starting with text are compared lexically. 22 23emulate -L zsh 24 25local IFS=".-" min_cnt=0 ver_cnt=0 part min_ver version order 26 27min_ver=(${=1}) 28version=(${=2:-$ZSH_VERSION} 0) 29 30while (( $min_cnt <= ${#min_ver} )); do 31 while [[ "$part" != <-> ]]; do 32 (( ++ver_cnt > ${#version} )) && return 0 33 if [[ ${version[ver_cnt]} = *[0-9][^0-9]* ]]; then 34 # Contains a number followed by text. Not a zsh version string. 35 order=( ${version[ver_cnt]} ${min_ver[ver_cnt]} ) 36 if [[ ${version[ver_cnt]} = <->* ]]; then 37 # Leading digits, compare by sorting with numeric order. 38 [[ $order != ${${(On)order}} ]] && return 1 39 else 40 # No leading digits, compare by sorting in lexical order. 41 [[ $order != ${${(O)order}} ]] && return 1 42 fi 43 [[ $order[1] != $order[2] ]] && return 0 44 fi 45 part=${version[ver_cnt]##*[^0-9]} 46 done 47 48 while true; do 49 (( ++min_cnt > ${#min_ver} )) && return 0 50 [[ ${min_ver[min_cnt]} = <-> ]] && break 51 done 52 53 (( part > min_ver[min_cnt] )) && return 0 54 (( part < min_ver[min_cnt] )) && return 1 55 part='' 56done 57