Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 条件表达式如何比较字符串?_Bash - Fatal编程技术网

Bash 条件表达式如何比较字符串?

Bash 条件表达式如何比较字符串?,bash,Bash,bash--version:gnubash,version 4.2.45(1)-发布(x86_64-pc-linux-GNU) uname-a:Linux-linuxmint 3.8.0-19-genericBash手册上说: 与[[]一起使用时,运算符使用当前语言环境按字典顺序进行排序。test命令使用ASCII排序 这归结为分别使用strcoll(3)或strcmp(3) 使用以下程序(strcoll\u strcmp.c)对此进行测试: Using conditional expressi

bash--version
gnubash,version 4.2.45(1)-发布(x86_64-pc-linux-GNU)

uname-a
Linux-linuxmint 3.8.0-19-generic

Bash手册上说:

与[[]一起使用时,<和>运算符使用当前语言环境按字典顺序进行排序。test命令使用ASCII排序

这归结为分别使用strcoll(3)或strcmp(3)

使用以下程序(strcoll\u strcmp.c)对此进行测试:

Using conditional expression:
ok
not ok
Using test:
ok
ok

确切地说,我不知道为什么会这样比较。这一定是由于一些英语词典排序规则。我认为确切的规则在和随附的模板表中有描述。Glibc在源代码树中的
libc/localedata/locales

下包含这些数据
可以解释您观察到的行为由以下人员从中定义:

您似乎在寻找基于ASCII排序规则的比较。您可以通过设置
compat32
compat40
来更改行为

bash-4.1 and later use the current locale’s collation sequence and strcoll(3).
$cat测试
shopt-s-40
echo“使用条件表达式:”
['<'0']&&echo正常| | echo不正常
[['a'<'0a']]和&echo正常| | echo不正常
echo“使用测试:”
[''\<'0']&回音正常| |回音不正常
['a'\<'0a']&回音正常| |回音不正常
$bash测试
使用条件表达式:
好啊
好啊
使用测试:
好啊
好啊
从手册中:

$ cat test
shopt -s compat40
echo 'Using conditional expression:'
[[ ' ' < '0' ]] && echo ok || echo not ok
[[ ' a' < '0a' ]] && echo ok || echo not ok
echo 'Using test:'
[ ' ' \< '0' ] && echo ok || echo not ok
[ ' a' \< '0a' ] && echo ok || echo not ok
$ bash test
Using conditional expression:
ok
ok
Using test:
ok
ok
32
如果已设置,则在使用“[[]”条件命令的“”运算符时,Bash会将其行为更改为3.2版的语言环境特定字符串比较行为。Bash-4.0之前的Bash版本使用ASCII排序规则和strcmp(3);Bash-4.1及更高版本使用当前语言环境的排序规则序列和strcoll(3)。
同胞40
如果设置了,Bash将在使用'[['条件命令的''运算符(请参见上一项)时,将其行为更改为4.0版的行为,以进行特定于语言环境的字符串比较,并产生中断命令列表的效果。

但是为什么在这两种情况下单个空格都小于“a”?@updogliu在我的答案底部看到了一个加法。我尝试了几个例子。似乎在strcoll
strcoll
中,前导空格被简单地丢弃了。这可能和这个例子一样奇怪…@updogliu丢弃空格是有意义的。这都与排序词典有关,电话簿等,这是高度依赖于语言和习俗。
$ LC_ALL=C ./strcoll_strcmp ' a' '0a'
strcoll(' a', '0a'): -16
strcmp(' a', '0a'): -16

$ LC_ALL=en_US.UTF-8 ./strcoll_strcmp ' a' '0a'
strcoll(' a', '0a'): 10
strcmp(' a', '0a'): -16
[ 'a' \< 'b' ] && echo ok
[[ 'a' < 'b' ]] && echo ok
[ ' a' \< '0a' ]
[[ ' a' < '0a' ]]
bash-4.1 and later use the current locale’s collation sequence and strcoll(3).
$ cat test
shopt -s compat40
echo 'Using conditional expression:'
[[ ' ' < '0' ]] && echo ok || echo not ok
[[ ' a' < '0a' ]] && echo ok || echo not ok
echo 'Using test:'
[ ' ' \< '0' ] && echo ok || echo not ok
[ ' a' \< '0a' ] && echo ok || echo not ok
$ bash test
Using conditional expression:
ok
ok
Using test:
ok
ok
compat32
If set, Bash changes its behavior to that of version 3.2 with respect to locale-specific string comparison when using the ‘[[’ conditional command’s ‘<’ and ‘>’ operators. Bash versions prior to bash-4.0 use ASCII collation and strcmp(3); bash-4.1 and later use the current locale’s collation sequence and strcoll(3). 
compat40
If set, Bash changes its behavior to that of version 4.0 with respect to locale-specific string comparison when using the ‘[[’ conditional command’s ‘<’ and ‘>’ operators (see previous item) and the effect of interrupting a command list.