加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

shell字符串包含的5种判断方法

(2023-01-05 16:10:41)
标签:

字符串

包含

判断

分类: shell


shell字符串包含的5种判断方法:

方法一:

strA="long string"

strB="string"

result=$(echo $strA | grep "${strB}")

if [[ "$result" != "" ]]

then

    echo "包含"

else

    echo "不包含"

fi



方法二(亲测可以):

strA="helloworld"

strB="low"

if [[ $strA =~ $strB ]]

then

    echo "包含"

else

    echo "不包含"

fi



方法三:



A="helloworld"

B="low"

if [[ $A == *$B* ]]

then

    echo "包含"

else

    echo "不包含"

fi



方法四:

thisString="1 2 3 4 5" # 源字符串

searchString="1 2" # 搜索字符串

case $thisString in

    *"$searchString"*) echo "包含" ;;

    *) echo "不包含" ;;

esac

0

阅读 收藏 喜欢 打印举报/Report
前一篇:jenkins安装
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有