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

1.使用grep

复制代码
s1=“abcdefg”
s2=“bcd”
result=$(echo s 1 g r e p " s1 | grep " {s2}")
if [[ “ r e s u l t " ! = " " ] ] t h e n e c h o " result" != "" ]] then echo " s1 include $s2”
else
echo “$1 not include $s2”
fi
复制代码
2. 使用操作符~

复制代码
fileName=/home/sss/data/hk

if [[ f i l e N a m e =   h k ] ] t h e n e c h o " fileName =~hk ]] then echo " fileName include hk"
else
echo “not include”
fi
复制代码
3. 使用通配符*

复制代码
A=“helloworld”
B=“low”
if [[ A = = A == * B* ]]
then
echo “包含”
else
echo “不包含”
fi
复制代码

猜你喜欢

转载自blog.csdn.net/qq123aa2006/article/details/88870236