Build a shell script comparable version number


/ Published in: Bash
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. #!/bin/sh
  2.  
  3. # put a version number "1.2.3.4" in the form "1 2 3 4"
  4. filter_version()
  5. {
  6. echo $1 | sed 's/\./ /g'
  7. }
  8.  
  9. # reverse the order: "1 2 3 4" become "4 3 2 1"
  10. reverse_order()
  11. {
  12. for n in $@
  13. do
  14. reverse="$n $reverse";
  15. done
  16.  
  17. echo $reverse
  18. }
  19.  
  20. # build an integer version number
  21. build_version()
  22. {
  23. multiplier=1
  24. version=0;
  25.  
  26. for i in $@
  27. do
  28. temp=$(( $multiplier * $i ))
  29. version=$(( $version + $temp ))
  30. multiplier=$(( $multiplier * 1000 ))
  31. done
  32.  
  33. echo $version
  34. }
  35.  
  36. # example
  37. VERSION="1.2.3.4"
  38. filtered=$(filter_version $VERSION)
  39. reversed=$(reverse_order $filtered)
  40.  
  41. build_version $reversed

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.