/ Published in: Bash
A \"simple\" way to build a version number to compare in your shell scripts
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/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