Skip to main content
added 38 characters in body
Source Link
codeforester
  • 43.8k
  • 21
  • 122
  • 159

Here is a simple Bash function that uses no external commands. It works for version strings that have up to three componentsnumeric parts in them - less than 3 is fine as well. It can be easily be extended for more. It implements =, <, <=, >, >=, and != conditions.

Here is a simple function that works for version strings that have up to three components in them - less than 3 is fine as well. It can be easily extended for more. It implements =, <, <=, >, >=, and != conditions.

Here is a simple Bash function that uses no external commands. It works for version strings that have up to three numeric parts in them - less than 3 is fine as well. It can easily be extended for more. It implements =, <, <=, >, >=, and != conditions.

added 39 characters in body
Source Link
codeforester
  • 43.8k
  • 21
  • 122
  • 159

Here is a simple function that works for version strings that have up to three components in them - less than 3 is fine as well. It can be easily extended for more. It implements =, <, <=, >, >=, and != conditions.

Here is a simple function that works for version strings that have up to three components in them. It can be easily extended. It implements =, <, <=, >, >=, and != conditions.

Here is a simple function that works for version strings that have up to three components in them - less than 3 is fine as well. It can be easily extended for more. It implements =, <, <=, >, >=, and != conditions.

added 61 characters in body
Source Link
codeforester
  • 43.8k
  • 21
  • 122
  • 159

Here is a pure Bash versionsimple function that works for version strings that have up to three components in them. It can be easily extended. It also supports a wildcardimplements *=, <, <=, >, >=, and != conditions.

#!/bin/bash
vercmp() {
    version1=$1
    version2=$2
    condition=$3
 
    [[ $version1 = '*' || $version2 = '*' ]] && return 0

    IFS=. v1_array=($version1) v2_array=($version2)
    v1=$((v1_array[0] * 100 + v1_array[1] * 10 + v1_array[2]))
    v2=$((v2_array[0] * 100 + v2_array[1] * 10 + v2_array[2]))
    diff=$((v2 - v1))
    [[ $condition = '='  ]] && ((diff == 0)) && return 0
    [[ $condition = '!=' ]] && ((diff != 0)) && return 0
    [[ $condition = '<'  ]] && ((diff >  0)) && return 0
    [[ $condition = '<=' ]] && ((diff >= 0)) && return 0
    [[ $condition = '>'  ]] && ((diff <  0)) && return 0
    [[ $condition = '>=' ]] && ((diff <= 0)) && return 0
    return 1
}

Here is a pure Bash version that works for version strings that have up to three components in them. It can be easily extended. It also supports a wildcard *.

#!/bin/bash
vercmp() {
    version1=$1
    version2=$2
    condition=$3
 
    [[ $version1 = '*' || $version2 = '*' ]] && return 0

    IFS=. v1_array=($version1) v2_array=($version2)
    v1=$((v1_array[0] * 100 + v1_array[1] * 10 + v1_array[2]))
    v2=$((v2_array[0] * 100 + v2_array[1] * 10 + v2_array[2]))
    diff=$((v2 - v1))
    [[ $condition = '='  ]] && ((diff == 0)) && return 0
    [[ $condition = '!=' ]] && ((diff != 0)) && return 0
    [[ $condition = '<'  ]] && ((diff >  0)) && return 0
    [[ $condition = '<=' ]] && ((diff >= 0)) && return 0
    [[ $condition = '>'  ]] && ((diff <  0)) && return 0
    [[ $condition = '>=' ]] && ((diff <= 0)) && return 0
    return 1
}

Here is a simple function that works for version strings that have up to three components in them. It can be easily extended. It implements =, <, <=, >, >=, and != conditions.

#!/bin/bash
vercmp() {
    version1=$1 version2=$2 condition=$3
    
    IFS=. v1_array=($version1) v2_array=($version2)
    v1=$((v1_array[0] * 100 + v1_array[1] * 10 + v1_array[2]))
    v2=$((v2_array[0] * 100 + v2_array[1] * 10 + v2_array[2]))
    diff=$((v2 - v1))
    [[ $condition = '='  ]] && ((diff == 0)) && return 0
    [[ $condition = '!=' ]] && ((diff != 0)) && return 0
    [[ $condition = '<'  ]] && ((diff >  0)) && return 0
    [[ $condition = '<=' ]] && ((diff >= 0)) && return 0
    [[ $condition = '>'  ]] && ((diff <  0)) && return 0
    [[ $condition = '>=' ]] && ((diff <= 0)) && return 0
    return 1
}
added 53 characters in body
Source Link
codeforester
  • 43.8k
  • 21
  • 122
  • 159
Loading
Source Link
codeforester
  • 43.8k
  • 21
  • 122
  • 159
Loading