2

I have a folder with scripts with a name pattern of UPDATE[x.y.z] where x.y.z is the script's version.

What I need is a bash script that runs the scripts ordered by their version, hence alphabetic sort is not good.

For example UPDATE1.11.0 should be executed after UPDATE1.2.3.

is there a comparator I can use on order to dictate that sorting order? if not, how else can it be done?

ghoti
  • 45,319
  • 8
  • 65
  • 104
user49204
  • 381
  • 1
  • 6
  • 14

2 Answers2

1

If you have GNU sort or another version that supports it, you can use version sort.

sort -V

or

sort --version-sort

Also, in my answer here, I posted a script which compares versions.

Community
  • 1
  • 1
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
1

And if you don't have GNU sort (i.e. you're in OSX or FreeBSD or NetBSD), you may be able to fake it by sorting different fields.

[ghoti ~]$ printf "foo1.2.3\nfoo1.11.0\nfoo1.4.1\nfoo1.4.0\n" | sort -nt. -k2,3
foo1.2.3
foo1.4.0
foo1.4.1
foo1.11.0
[ghoti ~]$

This misses out on the major version number because it's not delimited by a dot. But it may work for you anyway.

ghoti
  • 45,319
  • 8
  • 65
  • 104