如果结果不为空,说明strA包含strB。如果结果为空,说明不包含。 这个方法充分利用了grep 的特性,最为简洁。 方法二:利用字符串运算符 strA="helloworld" strB="low" if [[ $strA =~ $strB ]] then echo "包含" else echo "不包含" fi 利用字符串运算符 =~ 直接判断strA是否包含strB。(这不是比第一个...
在Shell中,判断一个字符串是否包含另一个字符串,可以使用多种方法。以下是几种常用的方法,每种方法都附有相应的代码示例和解释: 1. 使用grep命令 bash str="hello world" sub_str="world" if echo "$str" | grep -q "$sub_str"; then echo "包含" else echo "不包含" fi 解释:通过管道将字符串...
echo"包含"elseecho"不包含"fi thisString="1 2 3 4 5"# 源字符串 searchString="1 2"# 搜索字符串case$thisStringin*"$searchString"*) echo"包含";;*) echo"不包含";; esac STRING_A=$1STRING_B=$2if[[ ${STRING_A/${STRING_B}//} == $STRING_A ]];then##isnot substring. echo"包...
2.使用操作符~ fileName=/home/baidu/data/myfile if [[ $fileName =~myfile ]] then echo "$fileName include myfile" else echo "not include" fi 3.使用通配符* A="xiaoming" B="xiao" if [[ $A == *$B* ]] then echo "包含" else echo "不包含" fi发布...
echo "包含" else echo "不包含" fi thisString="1 2 3 4 5" # 源字符串 searchString="1 2" # 搜索字符串 case $thisString in *"$searchString"*) echo "包含" ;; *) echo "不包含" ;; esac STRING_A=$1 STRING_B=$2 if [[ ${STRING_A/${STRING_B}//} == $STRING_A ]];then...
可以使用`grep`命令来判断一个字符串是否包含在另一个字符串中。例如:```shellif echo "$str" | grep -q "$sub_str"; then echo "包...
/bin/bash#shell判断字符串包含关系#判断strA是否包含strB?# 1.利用grep查找grep_search(){strA="testA,testB"strB="testB"result=`echo $strA|grep $strB`if[${result}x!=""x];thenecho'include'elseecho'exclusive'fi}# 2.利用字符串运算符查找operator_search(){strA="testA,testB"strB="testC"if...
shell 判断字符串是否包含另一个字符串,使用greps1="abcdefg"s2="bcd"result=$(echo$s1|grep"${s2}")if[["$result"!=""]]thenecho"$s1include$s2"elseecho"$1notinclude$s2"