首页 > 系统服务 > 详细

shell 判断字符串是否包含另一个字符串

时间:2020-12-16 12:10:01      阅读:722      评论:0      收藏:0      [点我收藏+]

1.使用grep

s1="helloworld"
s2="ello"
result=$(echo $s1 | grep "${s2}")
if [[ "$result" != "" ]]
then
    echo "$s1 include $s2"
else
    echo "$1 not include $s2"
fi

2. 使用操作符~

fileName=/home/sss/data/cl

if [[ $fileName =~cl ]]
then
    echo "$fileName include cl"
else
    echo "not include"
fi

3. 使用通配符*

A="helloworld"
B="low"
if [[ $A == *$B* ]]
then
    echo "包含"
else
    echo "不包含"
fi

shell 判断字符串是否包含另一个字符串

原文:https://www.cnblogs.com/cchilei/p/14142776.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!