开发者

Bash script and string compares

All,

I have 2 version strings, e.g. "2.0.13" 开发者_开发百科and "2.0.2". I need to compare the 2 versions and determine which is the higher version.

How does one do that using bash?


You can use sort -V (version sort):

echo -e "2.0.13\n2.0.2" | sort -V

results in:

2.0.2
2.0.13


From bmk

You can use sort -V (version sort)

That's the best answer if it works, but unfortunately not all sort commands have the -V option.

If yours doesn't, you'll have to switch to Perl. Newer versions of Perl allow for certain variables to be declared as versions by prefixing them with a lowercase "v". You can then compare them with the gt operator.

See Perldoc perldata for more detail.


If you don't have -V, I'm sure there should be a way to do this by combining:

  • -t . to make sort split fields on full stops
  • -n to make sort sort numerically
  • -k to define the fields to sort on

But i can't make it work!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜