下面将使用示例来介绍expr的用法,在介绍之前,需要注意三点:
(1).数值表达式("+ - * / %")和比较表达式("< <= = == != >= >")会先将两端的参数转换为数值,转换失败将报错。所以可借此来判断参数或变量是否为整数。 (2).expr中的很多符号需要转义或使用引号包围。 (3).所有操作符的两边,都需要有空格。 |
(1).“string : REGEX"字符串匹配示例。要输出匹配到的字符串结果,需要使用”(“和”)",否则返回的将是匹配到的字符串数量。
[root@xuexi ~]# expr abcde : 'ab\(.*\)'
cde
[root@xuexi ~]# expr abcde : 'ab\(.\)'
c
[root@xuexi ~]# expr abcde : 'ab.*'
5
[root@xuexi ~]# expr abcde : 'ab.'
3
[root@xuexi ~]# expr abcde : '.*cd*'
4
注意,由于REGEX中隐含了"^",所以使得匹配时都是从string首字符开始的。
[root@xuexi ~]# expr abcde : 'cd.*'
0
之所以为0,是因为真正的正则表达式是"^cd.*",而abcde不是c开头而是a开头的,所以无法匹配到任何结果。因此,任何字符串匹配时,都应该从首字符开始。
字符串匹配时,会先将两端参数转换为字符格式。
(2)."index string chars"用法示例。
该表达式是从string中搜索chars中某个字符的位置,这个字符是string中最靠前的字符。例如:
[root@xuexi ~]# expr index abcde dec
3
该命令将对字符串"dec"逐字符分解,首先分解得到第一个字符d,从abcde中搜索到d的位置为4,再分解得到第二个字符e,该字符在abcde中的位置为5,最后得到的字符是c,该字符在abcde中的位置为3。其中3是最靠前的字符,所以命令返回的结果为3。
[root@xuexi ~]# expr index abcde xdc
3
如果chars中的所有字符都不存在于string中,则返回0。
[root@xuexi ~]# expr index abcde 1
0
[root@xuexi ~]# expr index abcde 1x
0
(3)."substr string pos len"用法示例。
该表达式是从string中取出从pos位置开始长度为len的子字符串。如果pos或len为非正整数时,将返回空字符串。
[root@xuexi ~]# expr substr abcde 2 3
bcd
[root@xuexi ~]# expr substr abcde 2 4
bcde
[root@xuexi ~]# expr substr abcde 2 5
bcde
[root@xuexi ~]# expr substr abcde 2 0
[root@xuexi ~]# expr substr abcde 2 -1
(4)."length string"用法示例。该表达式是返回string的长度,其中string不允许为空,否则将报错,所以可以用来判断变量是否为空。
[root@xuexi ~]# expr length abcde
5
[root@xuexi ~]# expr length 111
3
[root@xuexi ~]# expr length $xxx
expr: syntax error
[root@xuexi ~]# if [ $? -ne 0 ];then echo '$xxx is null';fi
$xxx is null
(5)."+ token"用法示例。
expr中有些符号和关键字有特殊意义,如"match"、“index”、“length”,如果要让其成为字符,使用该表达式将任意token强制解析为普通字符串。
[root@xuexi ~]# expr index index d
expr: syntax error
[root@xuexi ~]# expr index length g
expr: syntax error
[root@xuexi ~]# expr index + length g
4
对值为关键字的变量来说,则无所谓。
[root@xuexi ~]# len=lenght
[root@xuexi ~]# expr index $len g
4
(6).算术运算用法示例。
[root@xuexi ~]# expr 1 + 2
3
[root@xuexi ~]# a=3
[root@xuexi ~]# b=4
[root@xuexi ~]# expr $a + $b
7
[root@xuexi ~]# expr 4 + $a
7
[root@xuexi ~]# expr $a - $b
-1
算术乘法符号"*"因为是shell的元字符,所以要转义,可以使用引号包围,或者使用反斜线。
[root@xuexi ~]# expr $a * $b
expr: syntax error
[root@xuexi ~]# expr $a '*' $b
12
[root@xuexi ~]# expr $a \* $b
12
[root@xuexi ~]# expr $b / $a # 除法只能取整数
1
[root@xuexi ~]# expr $b % $a
1
任意操作符两端都需要有空格,否则:
[root@xuexi ~]# expr 4+$a
4+3
[root@xuexi ~]# expr 4 +$a
expr: syntax error
由于expr在进行算术运算时,首先会将操作符两边的参数转换为整数,任意一端转换失败都将会报错,所以可以用来判断参数或变量是否为整数。
[root@xuexi ~]# expr $a + $c
expr: non-integer argument
[root@xuexi ~]# if [ $? != 0 ];then echo '$a or $c is non-integer';fi
$a or $c is non-integer
(7).比较操作符< <= = == != >= >用法示例。其中"<“和”>“是正则表达式正的锚定元字符,且”<"会被shell解析为重定向符号,所以需要转义或用引号包围。
这些操作符会首先会将两端的参数转换为数值,如果转换成功,则采用数值比较,如果转换失败,则按照字符集的排序规则进行字符大小比较。比较的结果若为true,则expr返回1,否则返回0。
[root@xuexi ~]# a=3
[root@xuexi ~]# expr $a = 1
0
[root@xuexi ~]# expr $a = 3
1
[root@xuexi ~]# expr $a \* 3 = 9
1
[root@xuexi ~]# expr abc \> ab
1
[root@xuexi ~]# expr akc \> ackd
1
(8).逻辑连接符号"&“和”|"用法示例。这两个符号都需要转义,或使用引号包围。
"&"表示如果两个参数都非0,则返回第一个参数,否则返回0。但任意一个参数为空,则expr报错。除非空字符串使用引号包围,则处理方法和0一样。 "|"表示如果第一个参数非0,则返回第一个参数的值,否则返回第二个参数。但如果任意一个参数为空,则expr报错。除非空字符串使用引号包围,则处理方法和0一样。 |
[root@xuexi ~]# expr $abc '|' 1
expr: syntax error
[root@xuexi ~]# expr "$abc" '|' 1
1
[root@xuexi ~]# expr "$abc" '&' 1
0
[root@xuexi ~]# expr $abc '&' 1
expr: syntax error
[root@xuexi ~]# expr 0 '&' abc
0
[root@xuexi ~]# expr abc '&' 0
0
[root@xuexi ~]# expr abc '|' 0
abc
[root@xuexi ~]# expr 0 '|' abc
abc
[root@xuexi ~]# expr abc '&' cde
abc
[root@xuexi ~]# expr abc '|' cde
abc