Is there any way to compare such strings on bash, e.g.: 2.4.5
and 2.8
and 2.4.5.1
?
相关问题
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- JQ: Select when attribute value exists in a bash a
- Error building gcc 4.8.3 from source: libstdc++.so
- Why should we check WIFEXITED after wait in order
if it's just about to know whether one version is lower than another I came up checking whether
sort --version-sort
changes the order of my version strings:Here's a pure Bash solution that supports revisions (e.g. '1.0-r1'), based on the answer posted by Dennis Williamson. It can easily be modified to support stuff like '-RC1' or extract the version from a more complex string by changing the regular expression.
For details regarding the implementation, please refer to in-code comments and/or enable the included debug code:
Here is a pure Bash version that doesn't require any external utilities:
Run the tests:
Used as such:
(from https://apple.stackexchange.com/a/123408/11374)
This is for at most 4 fields in the version.
Another approach(modified version of @joynes) that compares dotted versions as asked in the question
(i.e "1.2", "2.3.4", "1.0", "1.10.1", etc.).
The maximum number of positions has to be known in advance. The approach expects max 3 version positions.
example usage:
returns: 1 since 1.10.1 is bigger than 1.7
returns: 0 since 1.10.1 is lower than 1.11