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
前一篇:jenkins安装
										后一篇:shell中expect命令
					
 加载中…
加载中…