shell 正则匹配字符

[root@yyjk tmp]#cat a1.sh
aaa='20190401.zip'
if [[ "$aaa" =~ ^[0-9]{4}[0-9]{1,2}[0-9]{1,2}\.zip$ ]]; then
  echo 'aaaaaaaaaaaaaaa'
else
  echo 'bbbbbbbbbbbbbbbb'
fi
[root@yyjk tmp]#sh ./a1.sh
aaaaaaaaaaaaaaa



[root@yyjk tmp]#cat a1.sh
aaa='x20190401.zip'
if [[ "$aaa" =~ ^[0-9]{4}[0-9]{1,2}[0-9]{1,2}\.zip$ ]]; then
  echo 'aaaaaaaaaaaaaaa'
else
  echo 'bbbbbbbbbbbbbbbb'
fi
[root@yyjk tmp]#sh ./a1.sh
bbbbbbbbbbbbbbbb


[root@yyjk tmp]#cat a1.sh
aaa='20190401.zipx'
if [[ "$aaa" =~ ^[0-9]{4}[0-9]{1,2}[0-9]{1,2}\.zip$ ]]; then
  echo 'aaaaaaaaaaaaaaa'
else
  echo 'bbbbbbbbbbbbbbbb'
fi
[root@yyjk tmp]#sh ./a1.sh
bbbbbbbbbbbbbbbb

 

你可能感兴趣的:(Shell编程)