Return to Snippet

Revision: 33392
at October 8, 2010 10:49 by oznek


Updated Code
#!/bin/sh

# put a version number "1.2.3.4" in the form "1 2 3 4"
filter_version()
{
    echo $1 | sed 's/\./ /g'
}

# reverse the order: "1 2 3 4" become "4 3 2 1"
reverse_order()
{
    for n in $@
    do
        reverse="$n $reverse";
    done

    echo $reverse
}

# build an integer version number 
build_version()
{
    multiplier=1
    version=0;

    for i in $@
    do
        temp=$(( $multiplier * $i ))
        version=$(( $version + $temp ))
        multiplier=$(( $multiplier * 1000 ))
    done

    echo $version
}

# example
VERSION="1.2.3.4"
filtered=$(filter_version $VERSION)
reversed=$(reverse_order $filtered)

build_version $reversed

Revision: 33391
at October 8, 2010 10:38 by oznek


Initial Code
#!/bin/sh

# put a version number "1.2.3.4" in the form "1 2 3 4"
filter_version()
{
    echo $1 | head -1 |cut -d\  -f 2 | sed 's/\./ /g'
}

# reverse the order: "1 2 3 4" become "4 3 2 1"
reverse_order()
{
    for n in $@
    do
        reverse="$n $reverse";
    done

    echo $reverse
}

# build an integer version number 
build_version()
{
    multiplier=1
    version=0;

    for i in $@
    do
        temp=$(( $multiplier * $i ))
        version=$(( $version + $temp ))
        multiplier=$(( $multiplier * 1000 ))
    done

    echo $version
}

# example
VERSION="1.2.3.4"
filtered=$(filter_version $VERSION)
reversed=$(reverse_order $filtered)

build_version $reversed

Initial URL

                                

Initial Description
A \"simple\" way to build a version number to compare in your shell scripts

Initial Title
Build a shell script comparable version number

Initial Tags

                                

Initial Language
Bash