【问题标题】:How does testing if a string is 'greater' than another work in Bash?如何在 Bash 中测试一个字符串是否比另一个字符串“更大”?
【发布时间】:2012-08-12 22:23:30
【问题描述】:

在 Bash 中我可以编写以下测试

[[ "f" > "a" ]]

这将导致返回 0,即 true。 bash 实际上是如何执行这个字符串比较的?据我了解> 进行整数比较。它会尝试比较操作数的 ASCII 值吗?

【问题讨论】:

  • 是的,我怀疑它有一个多态交易,其中 > 表示一个有两个字符串的东西,另一个有两个数字。但是我不是经验丰富的 bash 编码器。

标签: bash numeric-conversion


【解决方案1】:

来自help test:

  STRING1 > STRING2
                 True if STRING1 sorts after STRING2 lexicographically.

在内部,bash 使用 strcoll() 或 strcmp():

else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
  {
    if (shell_compatibility_level > 40 && flags & TEST_LOCALE)
      return ((op[0] == '>') ? (strcoll (arg1, arg2) > 0) : (strcoll (arg1, arg2) < 0));
    else
      return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
  }

后者实际比较 ASCII 码,前者(在启用区域设置时使用)执行更具体的比较,适合在给定区域设置中排序。

【讨论】:

    【解决方案2】:

    这是一个字母比较(AI​​UI 的排序顺序可能会受到当前语言环境的影响)。它比较每个字符串的第一个字符,如果左边的值较高,则为真,如果较低,则为假;如果它们相同,则比较第二个字符等。

    这不与整数比较相同,因为您使用[[ 2 -gt 1 ]] 或(( 2 &gt; 1 ))。为了说明字符串和整数比较之间的区别,请考虑以下所有内容都是“正确的”:

    [[ 2 > 10 ]]     # because "2" comes after "1" in ASCII sort order
    [[ 10 -gt 2 ]]   # because 10 is a larger number than 2
    (( 10 > 2 ))     # ditto
    

    这里还有一些测试,字符串比较是正确的,但整数比较是错误的:

    [[ 05 < 5 ]]    # Because "0" comes before "5"
    [[ +5 < 0 ]]    # Because "+" comes before the digits
    [[ -0 < 0 ]]    # Because "-" comes before the digits
    [[ -1 < -2 ]]   # Because "-" doesn't change how the second character is compared
    

    【讨论】:

      【解决方案3】:

      是的,它比较 ascii 值,如果相等,则在下一个字符中重复比较。

      /* Copyright (C) 1991, 1996, 1997, 2003 Free Software Foundation, Inc. 
         This file is part of the GNU C Library. 
      
         The GNU C Library is free software; you can redistribute it and/or 
         modify it under the terms of the GNU Lesser General Public 
         License as published by the Free Software Foundation; either 
         version 2.1 of the License, or (at your option) any later version. 
      
         The GNU C Library is distributed in the hope that it will be useful, 
         but WITHOUT ANY WARRANTY; without even the implied warranty of 
         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
         Lesser General Public License for more details. 
      
         You should have received a copy of the GNU Lesser General Public 
         License along with the GNU C Library; if not, write to the Free 
         Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 
         02111-1307 USA.  */ 
      
      #include <string.h> 
      #include <memcopy.h> 
      
      #undef strcmp 
      
      /* Compare S1 and S2, returning less than, equal to or 
         greater than zero if S1 is lexicographically less than, 
         equal to or greater than S2.  */ 
      int 
      strcmp (p1, p2) 
           const char *p1; 
           const char *p2; 
      { 
        register const unsigned char *s1 = (const unsigned char *) p1; 
        register const unsigned char *s2 = (const unsigned char *) p2; 
        unsigned reg_char c1, c2; 
      
        do 
          { 
            c1 = (unsigned char) *s1++; 
            c2 = (unsigned char) *s2++; 
            if (c1 == '\0') 
              return c1 - c2; 
          } 
        while (c1 == c2); 
      
        return c1 - c2; 
      } 
      

      【讨论】:

      • 我不明白你为什么要粘贴一些几乎不相关的 strcmp() 函数。 bash 支持语言环境,在这种情况下,它使用strcoll() 来执行适合特定字符集的比较。
      • 你说得对,但是 strcmp 以更简单的形式说明了字符串比较。目的是展示如何比较一般的字符串,而不是具体的 bash 实现。方法在bash、python、perl、PHP、c、Java中都是一样的……
      • 更多...问题不在于函数的名称,而在于方法。
      猜你喜欢
      • 2015-06-18
      • 2013-03-03
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 2013-03-11
      • 2014-07-23
      相关资源
      最近更新 更多