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

regex – 如何在bash脚本中使用正则表达式?

发布时间:2020-12-15 16:46:36 所属栏目:安全 来源:网络整理
导读:我想使用正则表达式检查变量是否有有效的年份。阅读 bash manual我明白我可以使用operator =? 看看下面的例子,我会看到“不OK”,但我看到“确定”。我究竟做错了什么? i="test"if [ $i=~"200[78]" ]then echo "OK"else echo "not OK"fi 它在3.1和3.2之间
我想使用正则表达式检查变量是否有有效的年份。阅读 bash manual我明白我可以使用operator =?

看看下面的例子,我会看到“不OK”,但我看到“确定”。我究竟做错了什么?

i="test"
if [ $i=~"200[78]" ]
then
  echo "OK"
else
  echo "not OK"
fi
它在3.1和3.2之间改变:

This is a terse description of the new features added to bash-3.2 since the release of bash-3.1.

Quoting the string argument to the [[ command’s =~ operator now forces string matching,as with the other pattern-matching operators.

所以使用它不带引号,因此:

i="test"
if [[ $i =~ 200[78] ]] ; then
    echo "OK"
else
    echo "not OK"
fi

(编辑:李大同)

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

    推荐文章
      热点阅读