27

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

[[ "f" > "a" ]]

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

4

3 回答 3

20

来自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 代码,前者(在启用区域设置时使用)执行更具体的比较,适合在给定区域设置中进行排序。

于 2012-08-16T14:38:25.423 回答
8

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

整数比较不同,因为您使用[[ 2 -gt 1 ]]or (( 2 > 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
于 2012-08-16T14:38:44.767 回答
1

是的,它比较 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; 
} 
于 2012-08-16T14:41:43.040 回答