尝试了有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

最新文章

  1. AFNetworking 3.0 源码解读 总结(干货)(上)
  2. PHP基础面试题
  3. java设计模式之备忘录模式
  4. JAVA之Forward 和 Redirect的区别
  5. golang channel basic
  6. Java IO 之 OutputStream源码
  7. ant exec
  8. Ruby On Rails 环境搭建MySQL数据库连接
  9. ASP.NET运行原理
  10. js之createTextRange方法
  11. 基于Bootstrap 3.x的免费高级管理控制面板主题:AdminLTE
  12. Python学习日记day10------函数的命名空间、作用域与闭合函数
  13. python使用tesseract-ocr完成验证码识别(模型训练和使用部分)
  14. 手动部署LNMT(Nginx+Tomcat)并实现Nginx反向代理Tomcat
  15. Vue2 第四天学习(Vue的生命周期)
  16. Groovy常用编程知识点简明教程
  17. js获取height和width总结
  18. bootstrap中点击左边展开
  19. Monocular 集成harbor helm 仓库
  20. 怎么在一台电脑上同时启动多个tomcat

热门文章

  1. Java开发学习(四十三)----MyBatisPlus查询语句之查询投影
  2. linux 使用ACR122U-A9设备读写M1卡
  3. day20 关联查询与多表联查 & 子查询与union联合查询 & 数据库定义语言DDL
  4. editorial 专栏
  5. 使用Supervisor监控mysql
  6. 【SQL必知必会】SQL知识查缺补漏
  7. linux安装Erlang和Rabbitmq以及安装问题解决
  8. 布尔值、元组、集合、input、格式化输出、赋值、运算符
  9. SQL语句查询优化方法
  10. 【转载】EXCEL VBA 通过VBA中的Union合并多个Range选择区域