expr命令的一些小用法
用途:求表达式变量的值
语法: expr [Expression]
描述: expr 命令读入[Expression]参数,计算它的值,然后将结果写入到标准输出。
用例:
1.expr substr String1 startPosition Length
返回字符串’hello world’中从位置3开始的5个字符串长的字符串
expr substr 'hello world' 3 5
返回:hello
2.expr index String1 String2
返回字符串’ab’中的任何一个字符在’aabcdeabcc’中出现的第一个位置
expr index 'aabcdeabcc' 'ab'
返回:1
3.expr length String1
返回字符串’hello’的长度
expr length 'hello'
4.expr match ‘String1’ ‘String2’
如果String1和String2相等(忽略空格,出去空格后的字符串),则返回String的长度
如果String1和String2不等,则返回0
expr match 'hello' 'hello'
返回:5
expr match 'hello ' 'hello'
返回:5 (忽略空格)
expr match 'hello' 'hello world'
返回:0