2012-08-16 9 views
17

はbashでIは0を返すことになる次の試験Bash - 文字列が別の文字列よりも大きい場合はテストします。内部的にはどのように動作しますか?

[[ "f" > "a" ]] 

、すなわち真を書き込むことができます。 bashは実際にこの文字列比較をどのように実行しますか?私の理解から、>は整数比較を行います。オペランドのASCII値を比較しようとしていますか? help testから

+1

そうですね、多相取引が起こっていると思われます。>は2つの文字列を持つものと2つの数字を持つものを意味します。しかし、私は経験豊富なbashコーダーではありません。 – VoronoiPotato

答えて

8

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コード、前者(ロケールが有効になっているときに使用される)を行うと比較指定されたロケールでのソートに適したより具体的な比較。

7

アルファベット順の比較です(AIUIでは、現在のロケールの影響を受ける可能性があります)。各文字列の最初の文字を比較し、左の文字が高い場合は真、それが偽の場合は真です。それらが同じなら、それは第二の文字を比較し、など

これは、あなたが[[ 2 -gt 1 ]]((2 > 1))を使用しているため、整数の比較と同じではありません。文字列と整数の比較との違いを説明するために、次のすべてが「真」であることを考慮してください。

[[ 2 > 10 ]]  # because "2" comes before "1" in ASCII sort order 
[[ 10 -gt 2 ]] # because 10 is a larger number than 2 
((10 > 2))  # ditto 
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; 
} 
+2

ほとんど関係のない 'strcmp()'関数を貼り付ける理由はわかりません。 bashはロケールをサポートしています。この場合、代わりに 'strcoll()'を使って特定の文字セットに適した比較を実行します。 –

+0

あなたは正しいがstrcmpは文字列の比較を簡単な形で示している。目的は、特定のbash実装ではなく一般的な文字列を比較する方法を示します。メソッドは、bash、python、perl、PHP、c、Javaで同じです... – olivecoder

+0

その他...質問は関数名ではなくメソッドについてです。 – olivecoder

関連する問題