shell:判断某个变量是否包含字符串/变量的方法
2024-10-20 06:26:54
尝试了有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
最新文章
- AFNetworking 3.0 源码解读 总结(干货)(上)
- PHP基础面试题
- java设计模式之备忘录模式
- JAVA之Forward 和 Redirect的区别
- golang channel basic
- Java IO 之 OutputStream源码
- ant exec
- Ruby On Rails 环境搭建MySQL数据库连接
- ASP.NET运行原理
- js之createTextRange方法
- 基于Bootstrap 3.x的免费高级管理控制面板主题:AdminLTE
- Python学习日记day10------函数的命名空间、作用域与闭合函数
- python使用tesseract-ocr完成验证码识别(模型训练和使用部分)
- 手动部署LNMT(Nginx+Tomcat)并实现Nginx反向代理Tomcat
- Vue2 第四天学习(Vue的生命周期)
- Groovy常用编程知识点简明教程
- js获取height和width总结
- bootstrap中点击左边展开
- Monocular 集成harbor helm 仓库
- 怎么在一台电脑上同时启动多个tomcat
热门文章
- Java开发学习(四十三)----MyBatisPlus查询语句之查询投影
- linux 使用ACR122U-A9设备读写M1卡
- day20 关联查询与多表联查 &; 子查询与union联合查询 &; 数据库定义语言DDL
- editorial 专栏
- 使用Supervisor监控mysql
- 【SQL必知必会】SQL知识查缺补漏
- linux安装Erlang和Rabbitmq以及安装问题解决
- 布尔值、元组、集合、input、格式化输出、赋值、运算符
- SQL语句查询优化方法
- 【转载】EXCEL VBA 通过VBA中的Union合并多个Range选择区域