博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shell:判断某个变量是否包含字符串/变量的方法
阅读量:6542 次
发布时间:2019-06-24

本文共 2976 字,大约阅读时间需要 9 分钟。

尝试了有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";fiyes[clouder@ana53 bin]$ if [[ ${a2} =~ '.' ]];then echo "yes";else echo "no";fino[clouder@ana53 bin]$ if [[ ${a1} =~ "." ]];then echo "yes";else echo "no";fiyes[clouder@ana53 bin]$ if [[ ${a2} =~ "." ]];then echo "yes";else echo "no";fino[clouder@ana53 bin]$ if [[ ${a1} =~ "${b}" ]];then echo "yes";else echo "no";fiyes[clouder@ana53 bin]$ if [[ ${a2} =~ "${b}" ]];then echo "yes";else echo "no";fino

** 不能输出正确结果 **

[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";fiyes[clouder@ana53 bin]$ if [[ ${a2} =~ . ]];then echo "yes";else echo "no";fiyes[clouder@ana53 bin]$ if [[ ${a1} =~ ${b} ]];then echo "yes";else echo "no";fiyes[clouder@ana53 bin]$ if [[ ${a2} =~ ${b} ]];then echo "yes";else echo "no";fiyes[clouder@ana53 bin]$ if [[ ${a1} =~ '${b}' ]];then echo "yes";else echo "no";fino[clouder@ana53 bin]$ if [[ ${a2} =~ '${b}' ]];then echo "yes";else echo "no";fino

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";fiyes[clouder@ana53 bin]$ if [[ ${a2} == *.* ]];then echo "yes";else echo "no";fino

** 不能输出正确结果 ,通配符不能用括号括起来!**

[clouder@ana53 bin]$ a1='hello.world'[clouder@ana53 bin]$ a2='helloworld'[clouder@ana53 bin]$ if [[ ${a2} == "*.*" ]];then echo "yes";else echo "no";fino[clouder@ana53 bin]$ if [[ ${a1} == "*.*" ]];then echo "yes";else echo "no";fino[clouder@ana53 bin]$ if [[ ${a1} == '*.*' ]];then echo "yes";else echo "no";fino[clouder@ana53 bin]$ if [[ ${a2} == '*.*' ]];then echo "yes";else echo "no";fino

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";fino[clouder@ana53 bin]$ if ( echo ${a2} |grep -q a );then echo "yes";else echo "no";fino[clouder@ana53 bin]$ if ( echo ${a3} |grep -q a );then echo "yes";else echo "no";fiyes[clouder@ana53 bin]$ if ( echo ${a1} |grep -q 'a' );then echo "yes";else echo "no";fino[clouder@ana53 bin]$ if ( echo ${a2} |grep -q 'a' );then echo "yes";else echo "no";fino[clouder@ana53 bin]$ if ( echo ${a3} |grep -q 'a' );then echo "yes";else echo "no";fiyes[clouder@ana53 bin]$ if ( echo ${a1} |grep -q "a" );then echo "yes";else echo "no";fino[clouder@ana53 bin]$ if ( echo ${a2} |grep -q "a" );then echo "yes";else echo "no";fino[clouder@ana53 bin]$ if ( echo ${a3} |grep -q "a" );then echo "yes";else echo "no";fiyes

转载于:https://www.cnblogs.com/xiaozhuangAna/p/9726670.html

你可能感兴趣的文章
为什么有人讨厌 Google 的新 Logo?
查看>>
腾讯2017暑期实习编程题3
查看>>
Intellij IDEA 构建Spring Web项目 — 用户登录功能
查看>>
[AHOI2013]作业
查看>>
git push被忽略的文件 处理
查看>>
C#中用ILMerge将所有引用的DLL打成一个DLL文件
查看>>
PHP生成HTML静态页面
查看>>
Makefile 中:= ?= += =的区别【转】
查看>>
使用makecontext实现用户线程【转】
查看>>
Comet:基于 HTTP 长连接的“服务器推”技术
查看>>
BZOJ 2733: [HNOI2012]永无乡 启发式合并treap
查看>>
四种方法校验数组中是否包含某个指定的字符串
查看>>
29、Java并发性和多线程-非阻塞算法
查看>>
安装OpenResty开发环境
查看>>
第0课 从0开始
查看>>
hadoop无法启动DataNode问题
查看>>
java泛型中<?>和<T>区别
查看>>
这里是指推送通知跟NSNotification有区别:
查看>>
用户ID的代码生成
查看>>
win7经常出现“关闭xxxx前您必须关闭所有会话框”
查看>>