当前位置: 技术问答>linux和unix
如何使用Shell编写脚本不区分大小写比对字符串,谢谢!
来源: 互联网 发布时间:2015-07-19
本文导语: xxx=Temp if [ TEMP = "$xxx" ];then echo "PASS" else echo "FAIL" fi 我想不区分大小写比对,如何做到,谢谢! | 你可以将两个字符串统统转成大写(或小写), 再比较 #!/bin/bash xxx="Temp" yyy="temp" ...
xxx=Temp
if [ TEMP = "$xxx" ];then
echo "PASS"
else
echo "FAIL"
fi
我想不区分大小写比对,如何做到,谢谢!
if [ TEMP = "$xxx" ];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
#!/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
|
You can firstly transform string from upper case to lower case by tr toolkit.
|
还是用tr先转成全大写或者全小写再比较好。
|
用expr的话,得这样:
xxx=Temp
if [ `expr match $xxx "[T|t][E|e][M|m][P|p]$"` -ne 0 ]
then
echo "PASS"
else
echo "FAIL"
fi
xxx=Temp
if [ `expr match $xxx "[T|t][E|e][M|m][P|p]$"` -ne 0 ]
then
echo "PASS"
else
echo "FAIL"
fi