How to test strings for less than or equal?
In Bash, is there a simple way to test if one string is lexicographically less than or equal to another?
I know you can do:
if [[ "a" < "b" ]]
for testing strict inequality, or
if [[ 1 -le 1 ]]
for numbers. But -le
doesn't seem to work with strings, and using <=
gives a syntax error.
只是否定了大于测试:
if [[ ! "a" > "b" ]]
You need to use ||
with an additional condition instead of <=
:
[[ "$a" < "$b" || "$a" == "$b" ]]
You can flip the comparison and sign around and test negatively:
$ a="abc"
$ b="abc"
$ if ! [[ "$b" > "$a" ]] ; then echo "a <= b" ; fi
a <= b
If you want collating sequence of "A" then "a" then "B"... use:
shopt -s nocaseglob
链接地址: http://www.djcxy.com/p/17458.html
下一篇: 如何测试字符串小于或等于?