shell中判读字符串中是否有某字符(忽略大小写)

search_1='hello'
search_2='Hello'
str='helloworld'
if [[ $str =~ $search_1 ]]
then
   echo 'get search_1'
fi
if [[ $str =~ $search_2 ]]
then
   echo 'get search_2'
fi
#忽略大小写只是先转化为大小或小写
search_3=$(echo $search_2 | tr '[A-Z]' '[a-z]')
echo $search_3
if [[ $str =~ $search_3 ]]
then
   echo 'get search_3'
fi

这里需要重点了解 tr 命令的用法 http://man.linuxde.net/tr

猜你喜欢

转载自hbjava1985.iteye.com/blog/2391796