加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell:判断某个变量是否包含字符串/变量的方法

发布时间:2020-12-15 23:25:10 所属栏目:安全 来源:网络整理
导读:尝试了有3种方法: 1.使用“=~”符号,注意前后必须要有空格! ** 可以输出正确结果,被匹配的字符串必须要有引号括起来!** [[email?protected] bin]$ a1=‘hello.world‘[[email?protected] bin]$ a2=‘helloworld‘[[email?protected] bin]$ b=‘.‘[[emai

尝试了有3种方法:

1.使用“=~”符号,注意前后必须要有空格!

** 可以输出正确结果,被匹配的字符串必须要有引号括起来!**

[[email?protected] bin]$ a1=‘hello.world‘
[[email?protected] bin]$ a2=‘helloworld‘
[[email?protected] bin]$ b=‘.‘
[[email?protected] bin]$ if [[ ${a1} =~ ‘.‘ ]];then echo "yes";else echo "no";fi
yes
[[email?protected] bin]$ if [[ ${a2} =~ ‘.‘ ]];then echo "yes";else echo "no";fi
no
[[email?protected] bin]$ if [[ ${a1} =~ "." ]];then echo "yes";else echo "no";fi
yes
[[email?protected] bin]$ if [[ ${a2} =~ "." ]];then echo "yes";else echo "no";fi
no
[[email?protected] bin]$ if [[ ${a1} =~ "${b}" ]];then echo "yes";else echo "no";fi
yes
[[email?protected] bin]$ if [[ ${a2} =~ "${b}" ]];then echo "yes";else echo "no";fi
no

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

[[email?protected] bin]$ a1=‘hello.world‘
[[email?protected] bin]$ a2=‘helloworld‘
[[email?protected] bin]$ b=‘.‘
[[email?protected] bin]$ if [[ ${a1} =~ . ]];then echo "yes";else echo "no";fi
yes
[[email?protected] bin]$ if [[ ${a2} =~ . ]];then echo "yes";else echo "no";fi
yes
[[email?protected] bin]$ if [[ ${a1} =~ ${b} ]];then echo "yes";else echo "no";fi
yes
[[email?protected] bin]$ if [[ ${a2} =~ ${b} ]];then echo "yes";else echo "no";fi
yes
[[email?protected] bin]$ if [[ ${a1} =~ ‘${b}‘ ]];then echo "yes";else echo "no";fi
no
[[email?protected] bin]$ if [[ ${a2} =~ ‘${b}‘ ]];then echo "yes";else echo "no";fi
no

2.使用”==“加通配符wildcard,注意等号前后必须有空格,注意,通配符跟正则表达式有所区别,*表示匹配 0 或多个字符
可以输出正确结果

[[email?protected] bin]$ a1=‘hello.world‘
[[email?protected] bin]$ a2=‘helloworld‘
[[email?protected] bin]$ if [[ ${a1} == *.* ]];then echo "yes";else echo "no";fi
yes
[[email?protected] bin]$ if [[ ${a2} == *.* ]];then echo "yes";else echo "no";fi
no

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

[[email?protected] bin]$ a1=‘hello.world‘
[[email?protected] bin]$ a2=‘helloworld‘
[[email?protected] bin]$ if [[ ${a2} == "*.*" ]];then echo "yes";else echo "no";fi
no
[[email?protected] bin]$ if [[ ${a1} == "*.*" ]];then echo "yes";else echo "no";fi
no
[[email?protected] bin]$ if [[ ${a1} == ‘*.*‘ ]];then echo "yes";else echo "no";fi
no
[[email?protected] bin]$ if [[ ${a2} == ‘*.*‘ ]];then echo "yes";else echo "no";fi
no

3.使用echo + grep -q 选项

** 使用这种方法时匹配是否有"."会不正常,所以我们换成匹配普通字符,有没有括号都可以 **

[[email?protected] bin]$ a1=‘hello.world‘
[[email?protected] bin]$ a2=‘helloworld‘
[[email?protected] bin]$ a3="helloworlda"
[[email?protected] bin]$ if ( echo ${a1} |grep -q a );then echo "yes";else echo "no";fi
no
[[email?protected] bin]$ if ( echo ${a2} |grep -q a );then echo "yes";else echo "no";fi
no
[[email?protected] bin]$ if ( echo ${a3} |grep -q a );then echo "yes";else echo "no";fi
yes
[[email?protected] bin]$ if ( echo ${a1} |grep -q ‘a‘ );then echo "yes";else echo "no";fi
no
[[email?protected] bin]$ if ( echo ${a2} |grep -q ‘a‘ );then echo "yes";else echo "no";fi
no
[[email?protected] bin]$ if ( echo ${a3} |grep -q ‘a‘ );then echo "yes";else echo "no";fi
yes
[[email?protected] bin]$ if ( echo ${a1} |grep -q "a" );then echo "yes";else echo "no";fi
no
[[email?protected] bin]$ if ( echo ${a2} |grep -q "a" );then echo "yes";else echo "no";fi
no
[[email?protected] bin]$ if ( echo ${a3} |grep -q "a" );then echo "yes";else echo "no";fi
yes

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读