Shell脚本中判断字符串是否被包含在内

1、字段 grep
案例:
str1="abcdefgh"
str2="def"
result=$(echo $str1 | grep "${str2}")
if [[ "$result" != "" ]];then
    echo "包含"
else
    echo "不包含"
fi


2. 字符串运算符 =~:
案例:
str1="abcdefgh"
str2="def"
if [[ $str1 =~ $str2 ]];then
    echo "包含"
else
    echo "不包含"
fi


3、 正则表达式中的通配符 *:
案例:
str1="abcdefgh"
str2="def"
if [[ $str1 == *$str2* ]];then
    echo "包含"
else
    echo "不包含"
fi

 

.
.
.
.
游走在各发行版间老司机QQ群:905201396
不要嫌啰嗦的新手QQ群:756805267
Debian适应QQ群:912567610

 

posted @ 2019-08-28 17:38  e-8bit  阅读(31501)  评论(0编辑  收藏  举报