Skip to content

Instantly share code, notes, and snippets.

View karlkfi's full-sized avatar

Karl Isenberg karlkfi

View GitHub Profile
@karlkfi
karlkfi / semver-compare.sh
Created May 16, 2017 21:33
Compare two semantic versions in bash
# return 0 if version A >= version B using semanatic versioning
function semver_gte() {
VERISON_A="${1}"
VERISON_B="${2}"
SEMVER_PATTERN="[^0-9]*\([0-9][0-9]*\)[.]\([0-9][0-9]*\)[.]\([0-9][0-9]*\).*"
SEG1_A="$(echo "${VERISON_A}" | sed -e "s#${SEMVER_PATTERN}#\1#")"
SEG1_B="$(echo "${VERISON_B}" | sed -e "s#${SEMVER_PATTERN}#\1#")"
[[ ${SEG1_A} < ${SEG1_B} ]] && return 1
[[ ${SEG1_A} > ${SEG1_B} ]] && return 0
SEG2_A="$(echo "${VERISON_A}" | sed -e "s#${SEMVER_PATTERN}#\2#")"