zoukankan      html  css  js  c++  java
  • 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'
    [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
    
  • 相关阅读:
    js重要函数
    js判断是否为空
    checkbox选择框如果被选中value值就可以传过去,没有被选中value就不能穿过去(调试了近一天,坑爹的说)
    js常用点
    常用jdbc操作
    java.sql.SQLException: 索引中丢失 IN 或 OUT 参数:: 1
    com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 2 无效 待整理
    OOAD和UML
    .net 外部CSS文件不起作用总结
    线程读书笔记
  • 原文地址:https://www.cnblogs.com/xiaozhuangAna/p/9726670.html
Copyright © 2011-2022 走看看