不区分大小写如何比较shell脚本中的字符串
1个回答
展开全部
方法一:
你可以将两个字符串统统转成大写(或小写), 再比较
#!/bin/bash
xxx= "Temp "
yyy= "temp "
x_tmp=$(echo $xxx | tr [a-z] [A-Z])
y_tmp=$(echo $yyy | tr [a-z] [A-Z])
if [ "$x_tmp " = "$y_tmp " ];then
echo "PASS "
else
echo "FAIL "
fi
优点:通用
方法二:
用expr的话,得这样:
xxx=Temp
if [ `expr match $xxx "[T|t][E|e][M|m][P|p]$ "` -ne 0 ]
then
echo "PASS "
else
echo "FAIL "
fi
优点:高效,但是限制为特定字符序列
你可以将两个字符串统统转成大写(或小写), 再比较
#!/bin/bash
xxx= "Temp "
yyy= "temp "
x_tmp=$(echo $xxx | tr [a-z] [A-Z])
y_tmp=$(echo $yyy | tr [a-z] [A-Z])
if [ "$x_tmp " = "$y_tmp " ];then
echo "PASS "
else
echo "FAIL "
fi
优点:通用
方法二:
用expr的话,得这样:
xxx=Temp
if [ `expr match $xxx "[T|t][E|e][M|m][P|p]$ "` -ne 0 ]
then
echo "PASS "
else
echo "FAIL "
fi
优点:高效,但是限制为特定字符序列
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |