string shell 脚本中不区分大小写的字符串比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1728683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Case insensitive comparison of strings in shell script
提问by Sachin Chourasiya
The ==
operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?
该==
运算符用于比较 shell 脚本中的两个字符串。但是,我想比较两个字符串忽略大小写,怎么办?是否有任何标准命令?
采纳答案by ghostdog74
if you have bash
如果你有 bash
str1="MATCH"
str2="match"
shopt -s nocasematch
case "$str1" in
$str2 ) echo "match";;
*) echo "no match";;
esac
otherwise, you should tell us what shell you are using.
否则,您应该告诉我们您使用的是什么 shell。
alternative, using awk
替代方案,使用 awk
str1="MATCH"
str2="match"
awk -vs1="$str1" -vs2="$str2" 'BEGIN {
if ( tolower(s1) == tolower(s2) ){
print "match"
}
}'
回答by alphaniner
In Bash, you can use parameter expansion to modify a string to all lower-/upper-case:
在 Bash 中,您可以使用参数扩展将字符串修改为全部小写/大写:
var1=TesT
var2=tEst
echo ${var1,,} ${var2,,}
echo ${var1^^} ${var2^^}
回答by Riot
All of these answers ignore the easiest and quickest way to do this (as long as you have Bash 4):
所有这些答案都忽略了执行此操作的最简单快捷的方法(只要您有 Bash 4):
if [ "${var1,,}" = "${var2,,}" ]; then
echo ":)"
fi
All you're doing there is converting both strings to lowercase and comparing the results.
您所做的就是将两个字符串都转换为小写并比较结果。
回答by Gerry Hickman
Save the state of nocasematch (in case some other function is depending on it being disabled):
保存 nocasematch 的状态(以防其他功能取决于它被禁用):
local orig_nocasematch=$(shopt -p nocasematch)
shopt -s nocasematch
[[ "foo" == "Foo" ]] && echo "match" || echo "notmatch"
$orig_nocasematch
Note: only use local
if it's inside a function.
注意:只有local
在函数内部时才使用。
回答by Randy Proctor
One way would be to convert both strings to upper or lower:
一种方法是将两个字符串都转换为上限或下限:
test $(echo "string" | /bin/tr '[:upper:]' '[:lower:]') = $(echo "String" | /bin/tr '[:upper:]' '[:lower:]') && echo same || echo different
Another way would be to use grep:
另一种方法是使用 grep:
echo "string" | grep -qi '^String$' && echo same || echo different
回答by Ek C.
For korn shell, I use typeset built-in command (-l for lower-case and -u for upper-case).
对于 korn shell,我使用 typeset 内置命令(-l 表示小写,-u 表示大写)。
var=True
typeset -l var
if [[ $var == "true" ]]; then
print "match"
fi
回答by Cooper F. Nelson
Very easy if you fgrep to do a case-insensitive line compare:
如果您 fgrep 进行不区分大小写的行比较,则非常容易:
str1="MATCH"
str2="match"
if [[ $(fgrep -ix $str1 <<< $str2) ]]; then
echo "case-insensitive match";
fi
回答by stones333
Here is my solution using tr:
这是我使用 tr 的解决方案:
var1=match
var2=MATCH
var1=`echo $var1 | tr '[A-Z]' '[a-z]'`
var2=`echo $var2 | tr '[A-Z]' '[a-z]'`
if [ "$var1" = "$var2" ] ; then
echo "MATCH"
fi
回答by Larry
grep
has a -i
flag which means case insensitive so ask it to tell you if var2 is in var1.
grep
有一个-i
表示不区分大小写的标志,因此请它告诉您 var2 是否在 var1 中。
var1=match
var2=MATCH
if echo $var1 | grep -i "^${var2}$" > /dev/null ; then
echo "MATCH"
fi
回答by smac89
For zsh
the syntax is slightly different, but still shorter than most answers here:
因为zsh
语法略有不同,但仍然比这里的大多数答案短:
> str1='mAtCh'
> str2='MaTcH'
> [[ "$str1:u" = "$str2:u" ]] && echo 'Strings Match!'
Strings Match!
>
This will convert both strings to uppercase before the comparison.
这将在比较之前将两个字符串都转换为大写。
Another method makes use zsh's globbing flags
, which allows us to directly make use of case-insensitive matching by using the i
glob flag:
另一种方法使用 zsh's globbing flags
,它允许我们通过使用i
glob 标志直接使用不区分大小写的匹配:
setopt extendedglob
[[ $str1 = (#i)$str2 ]] && echo "Match success"
[[ $str1 = (#i)match ]] && echo "Match success"