Sac*_*iya 117 string shell compare case-insensitive
该==运算符用于比较的shell脚本两个字符串.但是,我想比较两个忽略大小写的字符串,怎么做呢?这有什么标准命令吗?
小智 137
在Bash中,您可以使用参数扩展将字符串修改为所有低/大写:
var1=TesT
var2=tEst
echo ${var1,,} ${var2,,}
echo ${var1^^} ${var2^^}
Run Code Online (Sandbox Code Playgroud)
Rio*_*iot 100
所有这些答案都忽略了最简单快捷的方法(只要你有Bash 4):
if [ "${var1,,}" = "${var2,,}" ]; then
echo ":)"
fi
Run Code Online (Sandbox Code Playgroud)
你所做的就是将两个字符串转换为小写并比较结果.
gho*_*g74 66
如果你有bash
str1="MATCH"
str2="match"
shopt -s nocasematch
case "$str1" in
$str2 ) echo "match";;
*) echo "no match";;
esac
Run Code Online (Sandbox Code Playgroud)
否则,你应该告诉我们你正在使用什么样的外壳.
替代方案,使用awk
str1="MATCH"
str2="match"
awk -vs1="$str1" -vs2="$str2" 'BEGIN {
if ( tolower(s1) == tolower(s2) ){
print "match"
}
}'
Run Code Online (Sandbox Code Playgroud)
小智 35
与ghostdog74的回答相同但代码略有不同
shopt -s nocasematch
[[ "foo" == "Foo" ]] && echo "match" || echo "notmatch"
shopt -u nocasematch
Run Code Online (Sandbox Code Playgroud)
Ran*_*tor 13
一种方法是将两个字符串转换为上限或下限:
test $(echo "string" | /bin/tr '[:upper:]' '[:lower:]') = $(echo "String" | /bin/tr '[:upper:]' '[:lower:]') && echo same || echo different
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用grep:
echo "string" | grep -qi '^String$' && echo same || echo different
Run Code Online (Sandbox Code Playgroud)
小智 10
我发现了这个很棒的博客/教程/有关处理区分大小写模式的内容。下面通过例子对三种方法进行详细说明:
1.使用tr命令将模式转换为小写
opt=$( tr '[:upper:]' '[:lower:]' <<<"$1" )
case $opt in
sql)
echo "Running mysql backup using mysqldump tool..."
;;
sync)
echo "Running backup using rsync tool..."
;;
tar)
echo "Running tape backup using tar tool..."
;;
*)
echo "Other options"
;;
esac
Run Code Online (Sandbox Code Playgroud)
2.谨慎使用 case 模式的通配符
opt=$1
case $opt in
[Ss][Qq][Ll])
echo "Running mysql backup using mysqldump tool..."
;;
[Ss][Yy][Nn][Cc])
echo "Running backup using rsync tool..."
;;
[Tt][Aa][Rr])
echo "Running tape backup using tar tool..."
;;
*)
echo "Other option"
;;
esac
Run Code Online (Sandbox Code Playgroud)
3.开启nocasematch
opt=$1
shopt -s nocasematch
case $opt in
sql)
echo "Running mysql backup using mysqldump tool..."
;;
sync)
echo "Running backup using rsync tool..."
;;
tar)
echo "Running tape backup using tar tool..."
;;
*)
echo "Other option"
;;
esac
shopt -u nocasematch
Run Code Online (Sandbox Code Playgroud)
小智 7
对于korn shell,我使用了排版内置命令(-l表示小写,-u表示大写).
var=True
typeset -l var
if [[ $var == "true" ]]; then
print "match"
fi
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您使用fgrep进行不区分大小写的行比较,则非常简单:
str1="MATCH"
str2="match"
if [[ $(fgrep -ix $str1 <<< $str2) ]]; then
echo "case-insensitive match";
fi
Run Code Online (Sandbox Code Playgroud)
因为zsh语法略有不同,但仍然比这里的大多数答案短:
> str1='mAtCh'
> str2='MaTcH'
> [[ "$str1:u" = "$str2:u" ]] && echo 'Strings Match!'
Strings Match!
>
Run Code Online (Sandbox Code Playgroud)
这将在比较之前将两个字符串都转换为大写。
另一种方法使用 zsh's globbing flags,它允许我们通过使用iglob 标志直接使用不区分大小写的匹配:
setopt extendedglob
[[ $str1 = (#i)$str2 ]] && echo "Match success"
[[ $str1 = (#i)match ]] && echo "Match success"
Run Code Online (Sandbox Code Playgroud)