ITeye博客

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

hbjava1985 · 2017-08-31 19:58 · shell · 编程语言 · Markdown


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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。