教训:某款嵌入式Linux [[不支持=~正则表达式
来源:互联网 发布:php 前台框架 编辑:程序博客网 时间:2024/06/08 04:14
Linux Bash 脚本中,通常使用 [[
判断正则表达式,例如判断非负整数。
if [[ '443089607' =~ ^[0-9]*$ ]]; then echo 'match'; else echo 'not match'; fiif [[ 'huzhenghui' =~ ^[0-9]*$ ]]; then echo 'match'; else echo 'not match'; fi
不过在某些嵌入式 Linux 中, [[
不支持 =~
正则表达式,返回错误信息:
sh: =~: unknown operand
此时,可以使用 expr
:
if expr '443089607' : ^[0-9]*$; then echo 'match'; else echo 'not match'; fiif expr 'huzhenghui' : ^[0-9]*$; then echo 'match'; else echo 'not match'; fi
不过可能会出现一个警告:
expr: warning: '^[0-9]*$': using '^' as the first characterof a basic regular expression is not portable; it is ignored
查看 info expr
可知:
‘STRING : REGEX’ Perform pattern matching. The arguments are converted to strings and the second is considered to be a (basic, a la GNU ‘grep’) regular expression, with a ‘^’ implicitly prepended. The first argument is then matched against this regular expression.
其中 with a ‘^’ implicitly prepended
也就是开头隐含了^
字符,不需要显式设置。应当写为:
if expr '443089607' : [0-9]*$; then echo 'match'; else echo 'not match'; fiif expr 'huzhenghui' : [0-9]*$; then echo 'match'; else echo 'not match'; fi
不说了,还有一大波的脚本需要逐个hack、调试……
阅读全文
0 0
- 教训:某款嵌入式Linux [[不支持=~正则表达式
- 正则表达式的一个教训
- Oracle不支持断言正则表达式?
- xsd正则表达式不支持^与$
- 嵌入式 正则表达式语法大全
- Linux正则表达式-表达式
- 嵌入式LINUX的开发经验与教训随笔
- 嵌入式LINUX的开发经验与教训随笔
- 嵌入式LINUX的开发经验与教训随笔
- linux 教训
- Linux正则表达式-编写正则表达式
- linux 正则表达式
- 正则表达式(linux/C++)
- Linux 正则表达式语法
- Linux之正则表达式
- linux c 正则表达式
- linux c 正则表达式
- linux C正则表达式
- CCPC-final 2016 B
- 解決所有软件Ctrl+Z快捷键失效的问题
- [C++] 通用算法、函数对象、lambda表达式、bind
- nginx php安装
- 有些MP4文件在浏览器无法正常显示
- 教训:某款嵌入式Linux [[不支持=~正则表达式
- Java加密解密之非对称加密
- 10.7 test solution.
- 安装redis
- 数据结构 树的表示,存储结构与遍历
- HTML5 Storage基础
- iptables默认规则配置
- openvpn服务器端搭建
- 【爬虫】使用八爪鱼爬行百度地图美食店数据