一、expr命令介绍
expr是一个用于数学表达式计算工具命令行工具,它可以对整数、小数和字符串进行算术操作、逻辑运算、比较操作和字符串操作等。
二、expr的命令帮助
1. expr的help帮助信息
expr的help帮助信息,如下欧式:
[root@docker ~]# expr --help
Usage: expr EXPRESSION
or: expr OPTION
--help display this help and exit
--version output version information and exit
Print the value of EXPRESSION to standard output. A blank line below
separates increasing precedence groups. EXPRESSION may be:
ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2
ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0
ARG1 < ARG2 ARG1 is less than ARG2
ARG1 <= ARG2 ARG1 is less than or equal to ARG2
ARG1 = ARG2 ARG1 is equal to ARG2
ARG1 != ARG2 ARG1 is unequal to ARG2
ARG1 >= ARG2 ARG1 is greater than or equal to ARG2
ARG1 > ARG2 ARG1 is greater than ARG2
ARG1 + ARG2 arithmetic sum of ARG1 and ARG2
ARG1 - ARG2 arithmetic difference of ARG1 and ARG2
ARG1 * ARG2 arithmetic product of ARG1 and ARG2
ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2
ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2
STRING : REGEXP anchored pattern match of REGEXP in STRING
match STRING REGEXP same as STRING : REGEXP
substr STRING POS LENGTH substring of STRING, POS counted from 1
index STRING CHARS index in STRING where any CHARS is found, or 0
length STRING length of STRING
+ TOKEN interpret TOKEN as a string, even if it is a
keyword like 'match' or an operator like '/'
( EXPRESSION ) value of EXPRESSION
Beware that many operators need to be escaped or quoted for shells.
Comparisons are arithmetic if both ARGs are numbers, else lexicographical.
Pattern matches return the string matched between \( and \) or null; if
\( and \) are not used, they return the number of characters matched or 0.
Exit status is 0 if EXPRESSION is neither null nor 0, 1 if EXPRESSION is null
or 0, 2 if EXPRESSION is syntactically invalid, and 3 if an error occurred.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'expr invocation'
2. expr的常用运算符
- expr的常用运算符
加法运算:+
减法运算:-
乘法运算:\*
除法运算:/
取模(取余)运算:%
三、expr命令的基本使用
1. 查询expr工具版本
查询expr工具版本
[root@docker ~]# expr --version
expr (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Parker, James Youngman, and Paul Eggert.
2. 算术运算
可以使用加、减、乘、除、模运算符进行基本的算术运算
- 加法运算
[root@docker ~]# expr 8 + 9
17
- 减法运算
[root@docker ~]# expr 8 - 9
-1
- 乘法运算
[root@docker ~]# expr 8 \* 9
72
- 除法运算
[root@docker ~]# expr 12 / 6
2
- 取余运算
[root@docker ~]# expr 13 % 6
1
3. 比较操作
使用expr进行比较操作,判断正确输出1,判断错误输出为0。
[root@docker ~]# expr 13 \< 20
1
[root@docker ~]# expr 25 \< 20
0
4. 字符串操作
可以使用字符串操作符对字符串进行操作,其中,:“:”表示正则匹配,正则表达式中,“.”表示任意字符,“*”表示匹配任意次数。
需要注意的是,在expr中,各种操作符与数字或字符串之间必须用空格隔开。而且expr对于特殊字符,需要进行转义处理(如上述例子中的“\”符号)。
[root@docker ~]# expr "hello" : 'he'
2
[root@docker ~]# expr "hello" : '.*'
5
四、注意事项
- 确保在
expr
中的操作符和操作数之间留有空格。 - 使用特殊字符(如
<
,>
,(
,)
,|
,&
)时要进行转义或用引号括起来。 - 字符串长度可以通过
expr length "string"
来计算。 - 对于字符串匹配,可以使用
expr index "string" "substring"
来找到子串的位置。 - 在执行算术运算时,确保使用正确的算术运算符并留有适当的空格。
- 比较操作中,
expr
返回1
表示条件为真,返回0
表示条件为假。 - 注意
expr
的退出状态码,成功执行表达式会返回0
,否则返回非零值。 - 避免使用
expr
进行浮点数运算,应考虑使用bc
或其他工具来处理浮点数。 - 当引用变量时,确保变量没有多余的空格或其他可能引起错误的字符。