1

コロン文字が間違っているように見えるので、簡単なテストを行いました:。スクリプトは文字を出力し、明らかにコロンが の後9に来ますが、文字列テストを実行すると、実際にはスラッシュの後に来るように見えます/。何を与える?

$ cat chartest.sh
#!/bin/sh
echo $LANG

for i in {33..126}; do
  printf -v hex "%x" "$i"
  printf "\x$hex"
done

echo
[[ : > 9 ]] && echo true || echo false
[[ : > / ]] && echo true || echo false

$ ./chartest.sh
en_US.UTF-8
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnop
qrstuvwxyz{|}~
false
true

古いテスト関数を使用すると、正しく動作することがわかりました

$ [ : \> 9 ] && echo true || echo false
true

関連性もある

http://tiswww.case.edu/php/chet/bash/COMPAT

4

1 に答える 1

2

When you compare strings with [[ > ]], bash uses locale-aware comparison. In en_US.UTF-8, most common punctuation comes before numbers, regardless of ASCII code (or Unicode code-point, for that matter). If you change your locale (or at least LC_COLLATE) to C or C.UTF-8, then you should find that collation works more the way you expect it to.

Note that locale-aware comparison is not so simple as just transliterating codes. I personally find it necessary to set my LANG to C, because otherwise the sort utility's handling of spaces wrecks many of my scripts. I have no idea why Ubuntu chooses to do this:

$ echo $LANG
en_GB.UTF-8
$ [[ "week night" > "wee knight" ]] && echo yes || echo no
yes
$ [[ "week light" > "wee knight" ]] && echo yes || echo no
no
于 2012-10-02T21:20:28.303 に答える