菜鸟笔记
提升您的技术认知

shell 判断字符串是否包含另一个字符串-ag真人游戏

1.使用grep

s1="abcdefg"
s2="bcd"
result=$(echo $s1 | grep "${s2}")
if [[ "$result" != "" ]]
then
    echo "$s1 include $s2"
else
    echo "$1 not include $s2"
fi

2. 使用操作符~

filename=/home/sss/data/hk
if [[ $filename =~hk ]]
then
    echo "$filename include hk"
else
    echo "not include"
fi

3. 使用通配符*

a="helloworld"
b="low"
if [[ $a == *$b* ]]
then
    echo "包含"
else
    echo "不包含"
fi
网站地图