#!/bin/ksh # # version_chk # Korn shell function # Compares two numeric strings to determine if one is greater than, less than, # or equal to the other. Does not handle non-numeric version strings. # AUTHOR="Mike Arnold " VERSION="200402030242" # # $Id: version_chk,v 1.3 2004/02/03 11:41:33 marnold Exp $ # Function to compare a version string and return =(0), >(1), or <(2). version_chk () { CURRENT=$1 BASELINE=$2 IFSTMP="$IFS" ; IFS="." set $CURRENT # Think SCCS C_RELEASE=${1:-0} C_LEVEL=${2:-0} C_BRANCH=${3:-0} C_SEQUENCE=${4:-0} if [ $DEBUG ]; then echo C_RELEASE=$C_RELEASE echo C_LEVEL=$C_LEVEL echo C_BRANCH=$C_BRANCH echo C_SEQUENCE=$C_SEQUENCE fi set $BASELINE # Think SCCS B_RELEASE=${1:-0} B_LEVEL=${2:-0} B_BRANCH=${3:-0} B_SEQUENCE=${4:-0} if [ $DEBUG ]; then echo B_RELEASE=$B_RELEASE echo B_LEVEL=$B_LEVEL echo B_BRANCH=$B_BRANCH echo B_SEQUENCE=$B_SEQUENCE fi IFS="$IFSTMP" # return =(0), >(1), or <(2). if [[ "$C_RELEASE" -eq "$B_RELEASE" && \ "$C_LEVEL" -eq "$B_LEVEL" && \ "$C_BRANCH" -eq "$B_BRANCH" && \ "$C_SEQUENCE" -eq "$B_SEQUENCE" ]]; then return 0 fi if [[ "$C_RELEASE" -gt "$B_RELEASE" ]]; then return 1 elif [[ "$C_LEVEL" -gt "$B_LEVEL" ]]; then return 1 elif [[ "$C_BRANCH" -gt "$B_BRANCH" ]]; then return 1 elif [[ "$C_SEQUENCE" -gt "$B_SEQUENCE" ]]; then return 1 fi if [[ "$C_RELEASE" -lt "$B_RELEASE" ]]; then return 2 elif [[ "$C_LEVEL" -lt "$B_LEVEL" ]]; then return 2 elif [[ "$C_BRANCH" -lt "$B_BRANCH" ]]; then return 2 elif [[ "$C_SEQUENCE" -lt "$B_SEQUENCE" ]]; then return 2 fi return 5 } # If the variable DEBUG is set, then turn on tracing. # http://www.research.att.com/lists/ast-users/2003/05/msg00009.html if [ $DEBUG ]; then # This will turn on the ksh xtrace option for mainline code set -x # This will turn on the ksh xtrace option for all functions typeset +f | while read F junk do typeset -ft $F done unset F junk fi version_chk $1 $2 RETURN=$? # return =(0), >(1), or <(2). if [[ $RETURN -eq 0 ]]; then RESULT="equal to" elif [[ $RETURN -eq 1 ]]; then RESULT="greater than" elif [[ $RETURN -eq 2 ]]; then RESULT="less than" fi echo "$1 is $RESULT $2"