Bash 4.1条件正则表达式与3.1的改变之处
2012-11-22 21:58
176 查看
工作中,同一脚本在bash 3.1下执行成功,但在4.1环境下失败。
查询相关资料,发现是3.2版本新增的特性:
引用会把正则表达式直接看作字符串:
当前有两种处理方式,可以解决这个问题:
将正则表达式声明为变量:
本例子来源于网络:
采用3.1兼容模式运行
去除兼容模式
参考文章:
【1】http://wiki.bash-hackers.org/syntax/ccmd/conditional_expression
【2】http://tiswww.case.edu/php/chet/bash/NEWS
【3】bash: regular-expression comparaison (=~) no longer works.
查询相关资料,发现是3.2版本新增的特性:
f. Quoting the string argument to the [[ command's =~ operator now forces string matching, as with the other pattern-matching operators.
引用会把正则表达式直接看作字符串:
[vimos@vimos 015 Bash]$ [[ "HEllo" =~ "^([[:upper:]]{2}[[:lower:]]*$)" ]] && echo match || echo no no [vimos@vimos 015 Bash]$ [[ "HEllo" =~ ^([[:upper:]]{2}[[:lower:]]*$) ]] && echo match || echo no match [vimos@vimos 015 Bash]$ [[ "^([[:upper:]]{2}[[:lower:]]*$)" =~ "^([[:upper:]]{2}[[:lower:]]*$)" ]] && echo match || echo no match
当前有两种处理方式,可以解决这个问题:
将正则表达式声明为变量:
本例子来源于网络:
[vimos@vimos 015 Bash]$ REG="^([[:upper:]]{2}[[:lower:]]*$)";[[ "HEllo" =~ $REG ]] && echo match || echo no match
采用3.1兼容模式运行
[vimos@vimos 015 Bash]$ shopt -s compat31 [vimos@vimos 015 Bash]$ [[ "HEllo" =~ "^([[:upper:]]{2}[[:lower:]]*$)" ]] && echo match || echo no match
去除兼容模式
[vimos@vimos 015 Bash]$ shopt -u compat31 [vimos@vimos 015 Bash]$ [[ "HEllo" =~ ^([[:upper:]]{2}[[:lower:]]*$) ]] && echo match || echo no match [vimos@vimos 015 Bash]$ [[ "HEllo" =~ "^([[:upper:]]{2}[[:lower:]]*$)" ]] && echo match || echo no no
参考文章:
【1】http://wiki.bash-hackers.org/syntax/ccmd/conditional_expression
【2】http://tiswww.case.edu/php/chet/bash/NEWS
【3】bash: regular-expression comparaison (=~) no longer works.
相关文章推荐
- 正则表达式-分支条件
- JS点击图片改变图片图径并用正则表达式取图片名的代码
- 正则表达式笔记1 分支条件 逻辑操作符 |
- Bash 脚本:正则表达式基础篇
- 正则表达式学习记录-分枝条件
- 正则表达式 学习笔记3.1
- Bash的条件表达式求值
- 正则表达式-积累的开始-条件判断
- 正则表达式的条件与分组的探索——基于python
- Linux上的文件管理类命令和bash 的基础特性及正则表达式
- bash & Makefile & 正则表达式学习
- 正则表达式实现条件替换功能
- bash正则表达式部分知识点
- 正则表达式-嵌入条件
- bash 中用于grep的正则表达式
- Bash 脚本:正则表达式基础篇
- 正则表达式的应用&按钮可执行状态的改变
- bash学习之九:正则表达式(regular expression)和grep
- Bash之正则表达式
- bash中常用正则表达式