#! /bin/bash # # Copyright 2009-2015 Yorba Foundation # # This software is licensed under the GNU LGPL (version 2.1 or later). # See the COPYING file in this distribution. # # chkver # # Returns 0 if queried version is greater than or equal (for min) or lesser than (for max) the # supplied value, or 1 otherwise. # # Set VERBOSE environment variable for somewhat useful output. # # NOTE: # This is an ultra-naive implementation with just enough error-checking. usage() { echo 'usage: chkver ' } abort() { usage exit 1 } verify_cardinal() { while [ $# != 0 ] do if [ $1 ] && [ $1 -eq $1 2> /dev/null ] && [ $1 -ge 0 ] then : else abort fi shift done } # check_min name number min-number check_min() { if [ $2 -gt $3 ] then verbose $1 large enough. exit 0 elif [ $2 -lt $3 ] then verbose $1 not large enough. exit 1 fi } # check_max name number max-number check_max() { if [ $2 -lt $3 ] then verbose $1 small enough. exit 0 elif [ $2 -gt $3 ] then verbose $1 not small enough. exit 1 fi } verbose() { if [ $VERBOSE ] then echo $* fi } # Check number of arguments if [ $# -lt 3 ] then abort fi # Parse arguments mode=$1 if [ $mode != "min" ] && [ $mode != "max" ] then abort fi major=`echo $2 | awk -F. '{print $1}'` minor=`echo $2 | awk -F. '{print $2}'` revision=`echo $2 | awk -F. '{print $3}'` min_major=`echo $3 | awk -F. '{print $1}'` min_minor=`echo $3 | awk -F. '{print $2}'` min_revision=`echo $3 | awk -F. '{print $3}'` # Verify they're all positive integers verify_cardinal "$major" "$minor" "$revision" "$min_major" "$min_minor" "$min_revision" verbose Comparing $major.$minor.$revision against $mode $min_major.$min_minor.$min_revision # check version numbers in order of importance if [ $mode == "min" ] then check_min "Major" $major $min_major check_min "Minor" $minor $min_minor check_min "Revision" $revision $min_revision else check_max "Major" $major $min_major check_max "Minor" $minor $min_minor check_max "Revision" $revision $min_revision fi verbose Same version. if [ $mode == "min" ] then exit 0 else exit 1 fi