Shell语言用例

bash的if语句用法之:模式匹配

2018-10-27  本文已影响0人  CodingCode

bash的if语句用法之:模式匹配

基本语法:

if [[ VAR =~ PATTERN ]]; then

注意有两点:

  1. 模式匹配操作符号: =~
  2. 用双中括号 [[和]]括起来;这是bash的增强语法。
  3. PATTERN不能使用引号("")括起来;这点看起来很奇怪,但我也不知道为什么。

举例说明:

#!/bin/bash

if [[  aabb  =~  ^aa  ]]; then echo "YES"; else echo "NO"; fi       # YES
if [[  aabb  =~ "^aa" ]]; then echo "YES"; else echo "NO"; fi       # NO
if [[  aabb  =~ "^aa" ]]; then echo "YES"; else echo "NO"; fi       # NO
if [[ "aabb" =~ "^aa" ]]; then echo "YES"; else echo "NO"; fi       # NO
if [[ "aabb" =~  ^aa  ]]; then echo "YES"; else echo "NO"; fi       # YES

VAR="aabb"
if [[  ${VAR}  =~  ^aa  ]]; then echo "YES"; else echo "NO"; fi     # YES
if [[  ${VAR}  =~ "^aa" ]]; then echo "YES"; else echo "NO"; fi     # NO
if [[  ${VAR}  =~ "^aa" ]]; then echo "YES"; else echo "NO"; fi     # NO
if [[ "${VAR}" =~ "^aa" ]]; then echo "YES"; else echo "NO"; fi     # NO
if [[ "${VAR}" =~  ^aa  ]]; then echo "YES"; else echo "NO"; fi     # YES

PAT="^aa"
if [[  ${VAR}  =~  ${PAT}  ]]; then echo "YES"; else echo "NO"; fi  # YES
if [[  ${VAR}  =~ "${PAT}" ]]; then echo "YES"; else echo "NO"; fi  # NO
if [[  ${VAR}  =~ "${PAT}" ]]; then echo "YES"; else echo "NO"; fi  # NO
if [[ "${VAR}" =~ "${PAT}" ]]; then echo "YES"; else echo "NO"; fi  # NO
if [[ "${VAR}" =~  ${PAT}  ]]; then echo "YES"; else echo "NO"; fi  # YES

上述例子看到,如果PATTER使用双引号的全部输出匹配失败。这个要区分和普通值比较的差异:

if [  aabb  ==  aabb  ]; then echo "YES"; else echo "NO"; fi        # YES
if [  aabb  == "aabb" ]; then echo "YES"; else echo "NO"; fi        # YES
if [ "aabb" == "aabb" ]; then echo "YES"; else echo "NO"; fi        # YES

VAR1=aabb
if [  ${VAR1}  ==  aabb  ]; then echo "YES"; else echo "NO"; fi     # YES
if [  ${VAR1}  == "aabb" ]; then echo "YES"; else echo "NO"; fi     # YES
if [ "${VAR1}" == "aabb" ]; then echo "YES"; else echo "NO"; fi     # YES

VAR2=aabb
if [  ${VAR1}  ==  ${VAR2}  ]; then echo "YES"; else echo "NO"; fi  # YES
if [  ${VAR1}  == "${VAR2}" ]; then echo "YES"; else echo "NO"; fi  # YES
if [ "${VAR1}" == "${VAR2}" ]; then echo "YES"; else echo "NO"; fi  # YES

可见普通字符串值得比较时,双引号并不起值得作用,只是起到边界界定的作用。

例子1:匹配字符串以XXX结尾

if [[ "aab" =~ bb$ ]]; then echo "YES"; else echo "NO"; fi   # YES
if [[ "aabb" =~ bb$ ]]; then echo "YES"; else echo "NO"; fi  # NO

例子2:匹配字符串或者关系,等于XXX或者YYY

if [[ "AA" =~ ^(AA|BB|CC)$ ]]; then echo "YES"; else echo "NO"; fi    # YES
if [[ "BB" =~ ^(AA|BB|CC)$ ]]; then echo "YES"; else echo "NO"; fi    # YES
if [[ "CC" =~ ^(AA|BB|CC)$ ]]; then echo "YES"; else echo "NO"; fi    # YES
if [[ "CAAC" =~ ^(AA|BB|CC)$ ]]; then echo "YES"; else echo "NO"; fi  # NO
if [[ "CAAC" =~ (AA|BB|CC) ]]; then echo "YES"; else echo "NO"; fi    # YES

例子3:取反,不匹配

VAR1="aabb"
VAR2="ccbb"
PAT="^aa"

if [[ ! ${VAR1} =~ ${PAT} ]]; then echo "YES"; else echo "NO"; fi     # NO
if [[ ! ${VAR2} =~ ${PAT} ]]; then echo "YES"; else echo "NO"; fi     # YES

例子4:匹配包含空格

例子中匹配字符串在aa和bb之间只能包含空格或者没有。

#!/bin/bash

VAR1="aabb"
VAR2="aaXXbb"
VAR3="aa  bb"

PAT1="^aa[ ]*bb$"
if [[ ${VAR1} =~ ${PAT1} ]]; then echo "YES"; else echo "NO"; fi       # YES
if [[ ${VAR2} =~ ${PAT1} ]]; then echo "YES"; else echo "NO"; fi       # NO
if [[ ${VAR3} =~ ${PAT1} ]]; then echo "YES"; else echo "NO"; fi       # YES

PAT2="^aa[ ]+bb$"
if [[ ${VAR1} =~ ${PAT2} ]]; then echo "YES"; else echo "NO"; fi       # NO
if [[ ${VAR2} =~ ${PAT2} ]]; then echo "YES"; else echo "NO"; fi       # NO
if [[ ${VAR3} =~ ${PAT2} ]]; then echo "YES"; else echo "NO"; fi       # YES

思考另一个问题,能不能直接PATTERN,而不使用变量呢:

if [[ ${VAR1} =~ ^aa[ ]+bb$ ]]; then echo "YES"; else echo "NO"; fi       # syntax error

这很合理啊,中间的空格导致语法解析错误。

./t.sh:行4: 条件表达式中有语法错误
./t.sh:行4: `]+bb$' 附近有语法错误
./t.sh:行4: `if [[ "${VAR1}" =~ ^aa[ ]+bb$ ]]; then echo "YES"; else echo "NO"; fi       # YES'

那能不能用双引号括起来呢?

if [[  ${VAR1}  =~ "^aa[ ]+bb$" ]]; then echo "YES"; else echo "NO"; fi # NO
if [[ "${VAR2}" =~ "^aa[ ]+bb$" ]]; then echo "YES"; else echo "NO"; fi # NO
if [[ "${VAR3}" =~ "^aa[ ]+bb$" ]]; then echo "YES"; else echo "NO"; fi # NO

语法是正确了,但是执行结果不对呢,验证我们前面的注意事项:即PATTER不能使用双引号括起来。
因为如果PATTERN里面包含特殊字符,必须先把PATTERN定义成变量,然后在if与语句里面引用这个变量。

上一篇 下一篇

猜你喜欢

热点阅读