shell:判断某个变量是否包含字符串变量的⽅法
尝试了有3种⽅法:
1.使⽤“=~”符号,注意前后必须要有空格!
** 可以输出正确结果,被匹配的字符串必须要有引号括起来!**
[clouder@ana53 bin]$ a1='hello.world'
[clouder@ana53 bin]$ a2='helloworld'
[clouder@ana53 bin]$ b='.'
[clouder@ana53 bin]$ if [[ ${a1} =~ '.' ]];then echo "yes";else echo "no";fi
yes
[clouder@ana53 bin]$ if [[ ${a2} =~ '.' ]];then echo "yes";else echo "no";fi
no
[clouder@ana53 bin]$ if [[ ${a1} =~ "." ]];then echo "yes";else echo "no";fi
yes
[clouder@ana53 bin]$ if [[ ${a2} =~ "." ]];then echo "yes";else echo "no";fi
no
[clouder@ana53 bin]$ if [[ ${a1} =~ "${b}" ]];then echo "yes";else echo "no";fi
yes
[clouder@ana53 bin]$ if [[ ${a2} =~ "${b}" ]];then echo "yes";else echo "no";fi
no
** 不能输出正确结果 **
[clouder@ana53 bin]$ a1='hello.world'
[clouder@ana53 bin]$ a2='helloworld'
shell 字符串长度
[clouder@ana53 bin]$ b='.'
[clouder@ana53 bin]$ if [[ ${a1} =~ . ]];then echo "yes";else echo "no";fi
yes
[clouder@ana53 bin]$ if [[ ${a2} =~ . ]];then echo "yes";else echo "no";fi
yes
[clouder@ana53 bin]$ if [[ ${a1} =~ ${b} ]];then echo "yes";else echo "no";fi
yes
[clouder@ana53 bin]$ if [[ ${a2} =~ ${b} ]];then echo "yes";else echo "no";fi
yes
[clouder@ana53 bin]$ if [[ ${a1} =~ '${b}' ]];then echo "yes";else echo "no";fi
no
[clouder@ana53 bin]$ if [[ ${a2} =~ '${b}' ]];then echo "yes";else echo "no";fi
no
2.使⽤”==“加通配符wildcard,注意等号前后必须有空格,注意,通配符跟正则表达式有所区别,*表⽰匹配 0 或多个字符
** 可以输出正确结果 **
[clouder@ana53 bin]$ a1='hello.world'
[clouder@ana53 bin]$ a2='helloworld'
[clouder@ana53 bin]$ if [[ ${a1} == *.* ]];then echo "yes";else echo "no";fi
yes
[clouder@ana53 bin]$ if [[ ${a2} == *.* ]];then echo "yes";else echo "no";fi
no
** 不能输出正确结果,通配符不能⽤括号括起来!**
[clouder@ana53 bin]$ a1='hello.world'
[clouder@ana53 bin]$ a2='helloworld'
[clouder@ana53 bin]$ if [[ ${a2} == "*.*" ]];then echo "yes";else echo "no";fi
no
[clouder@ana53 bin]$ if [[ ${a1} == "*.*" ]];then echo "yes";else echo "no";fi
no
[clouder@ana53 bin]$ if [[ ${a1} == '*.*' ]];then echo "yes";else echo "no";fi
no
[clouder@ana53 bin]$ if [[ ${a2} == '*.*' ]];then echo "yes";else echo "no";fi
no
3.使⽤echo + grep -q 选项
** 使⽤这种⽅法时匹配是否有"."会不正常,所以我们换成匹配普通字符,有没有括号都可以 **
[clouder@ana53 bin]$ a1='hello.world'
[clouder@ana53 bin]$ a2='helloworld'
[clouder@ana53 bin]$ a3="helloworlda"
[clouder@ana53 bin]$ if ( echo ${a1} |grep -q a );then echo "yes";else echo "no";fi
no
[clouder@ana53 bin]$ if ( echo ${a2} |grep -q a );then echo "yes";else echo "no";fi
no
[clouder@ana53 bin]$ if ( echo ${a3} |grep -q a );then echo "yes";else echo "no";fi
yes
[clouder@ana53 bin]$ if ( echo ${a1} |grep -q 'a' );then echo "yes";else echo "no";fi no
[clouder@ana53 bin]$ if ( echo ${a2} |grep -q 'a' );then echo "yes";else echo "no";fi no
[clouder@ana53 bin]$ if ( echo ${a3} |grep -q 'a' );then echo "yes";else echo "no";fi yes
[clouder@ana53 bin]$ if ( echo ${a1} |grep -q "a" );then echo "yes";else echo "no";fi no
[clouder@ana53 bin]$ if ( echo ${a2} |grep -q "a" );then echo "yes";else echo "no";fi no
[clouder@ana53 bin]$ if ( echo ${a3} |grep -q "a" );then echo "yes";else echo "no";fi yes
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论