使用Bash比较PHP版本号? [英] Comparing PHP version numbers using Bash?

查看:105
本文介绍了使用Bash比较PHP版本号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本,应该确保用户当前的PHP版本在一定范围内,尽管它应该工作,但是某个地方存在一个错误,使其认为该版本超出范围,有人可以看看吗?并告诉我该如何解决?

I have this script that should make sure that the users current PHP version is between a certain range, though it SHOULD work, there is a bug somewhere that makes it think that the version is out of range, could someone take a look and tell me what I can do to fix it?

function version { echo "$@" | gawk -F. '{ printf("%d.%d.%d\n", $1,$2,$3); }'; }

phpver=`php -v |grep -Eow '^PHP [^ ]+' |gawk '{ print $2 }'`

if [ $(version $phpver) > $(version 5.2.13) ] || [ $(version $phpver) < $(version 5.2.13) ]; then
  echo "PHP Version $phpver must be between 5.2.13 - 5.3.15"
  exit
fi

推荐答案

此处是比较版本的方法.

Here's how to compare versions.

使用sort -V:

using sort -V:

function version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }

示例用法:

first_version=5.100.2
second_version=5.1.2
if version_gt $first_version $second_version; then
     echo "$first_version is greater than $second_version !"
fi

pro:

  • 比较精美版本字符串的可靠方法:
    • 支持任意长度的子部分(例如:1.3alpha.2.dev2> 1.1吗?)
    • 支持字母顺序排列(即:1.alpha< 1.beta2)
    • 支持大尺寸版本(即1.10003939209329320932> 1.2039209378273789273吗?)
    • solid way to compare fancy version strings:
      • support any length of sub-parts (ie: 1.3alpha.2.dev2 > 1.1 ?)
      • support alpha-betical sort (ie: 1.alpha < 1.beta2)
      • support big size version (ie: 1.10003939209329320932 > 1.2039209378273789273 ?)
      • 通常非常有用,带有3个参数:(即:1.2< my_version< 2.7)

      缺点:

      • 使用许多不同的程序来调用不同的程序.因此效率不高.
      • 使用的是sort的较新版本,可能无法在您的计算机上使用 系统. (与man sort一起检查)
      • uses a lot of various calls to different programs. So it's not that efficient.
      • uses a pretty recent version of sort and it might not be available on your system. (check with man sort)

      没有sort -V:

      without sort -V:

      ## each separate version number must be less than 3 digit wide !
      function version { echo "$@" | gawk -F. '{ printf("%03d%03d%03d\n", $1,$2,$3); }'; }
      

      示例用法:

      first_version=5.100.2
      second_version=5.1.2
      if [ "$(version "$first_version")" -gt "$(version "$second_version")" ]; then
           echo "$first_version is greater than $second_version !"
      fi
      

      pro:

      • 快速解决方案,因为它仅调用1个子进程
      • 兼容得多的解决方案.

      缺点:

      • 非常特定的版本字符串必须:
        • 仅具有1、2或3个部分的版本. (不包括"2.1.3.1")
        • 每个部分都只能是数字(不包括"3.1a")
        • 每个部分不能大于999(不包括"1.20140417")
        • quite specific, version string must:
          • have version with 1, 2 or 3 parts only. (excludes '2.1.3.1')
          • each parts must be numerical only (excludes '3.1a')
          • each part can't be greater than 999 (excludes '1.20140417')

          关于脚本的评论:

          我看不到它如何工作:

            如注释中所述
          • ><是非常特殊的shell字符,您应将它们替换为-gt-lt
          • 即使替换了字符,也无法将版本号视为整数或浮点数.例如,在我的系统上,php版本是5.5.9-1ubuntu4.
          • as stated in a comment > and < are very special shell character and you should replace them by -gt and -lt
          • even if you replaced the characters, you can't compare version numbers as if they where integers or float. For instance, on my system, php version is 5.5.9-1ubuntu4.

          但是您的函数version()已经非常聪明地编写了,并且可以绕过经典问题,即按字母数字排序不会对数字进行数字排序(按字母顺序1 <11 <2,这在数字上是错误的)可以为您提供帮助.但是要小心:bash不支持任意大的数字(如果要与32位系统兼容,请尝试将数字保持在32位以下,因此该数字应为9位长).因此,我已经修改了您的代码(在第二种方法中,不使用sort -V)仅对版本字符串的每个部分强制使用3位数字.

          But your function version() is quite cleverly written already and may help you by circumventing the classical issue that sorting alphabetically numbers won't sort numbers numerically ( alphabetically 1 < 11 < 2, which is wrong numerically). But be carefull: arbitrarily large numbers aren't supported by bash (try to keep under 32bits if you aim at compatibility with 32bits systems, so that would be 9 digit long numbers). So I've modified your code (in the second method NOT using sort -V) to force only 3 digits for each part of the version string.

          编辑:应用了@phk改进,因为它非常聪明,并使用sort删除了第一个版本中的子流程调用.谢谢.

          EDIT: applied @phk amelioration, as it is noticeably cleverer and remove a subprocess call in the first version using sort. Thanks.

          这篇关于使用Bash比较PHP版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆