linux系统中expr命令

1、linux系统中expr命令实现命令行中的四则运算

  简单示例:

[root@linuxprobe test]# expr 5 + 3  ## 在命令行中实现加法运算
8

 

2、中间必须有空格


[root@linuxprobe test]# expr 5+3  ##中间必须有空格
5+3
[root@linuxprobe test]# expr 5 +3  ##同上
expr: syntax error: unexpected argument ‘+3’
[root@linuxprobe test]# expr 5+ 3  ## 同上
expr: syntax error: unexpected argument ‘3’

 

3、必须是整数运算

[root@linuxprobe test]# expr 5 + 3
8
[root@linuxprobe test]# expr 5.5 + 3 ## 必须是整数运算
expr: non-integer argument

 

4、减法运算

[root@linuxprobe test]# expr 10 - 4
6
[root@linuxprobe test]# expr 10 + 5 - 2
13

 

5、乘法运算

[root@linuxprobe test]# ls
[root@linuxprobe test]# expr 3 * 5  ## 当前目录为空时,*前可以不加转义
15
[root@linuxprobe test]# touch a.txt
[root@linuxprobe test]# ls
a.txt
[root@linuxprobe test]# expr 3 * 5
expr: syntax error: unexpected argument ‘a.txt’
[root@linuxprobe test]# expr 3 \* 5  ## 加转义
15

 

6、除法、取余

[root@linuxprobe test]# expr 10 / 2  
5
[root@linuxprobe test]# expr 10 / 3 ## 除法只保留整数
3
[root@linuxprobe test]# expr 10 % 3 ##取余数
1

 

posted @ 2020-10-13 21:19  小鲨鱼2018  阅读(1280)  评论(0编辑  收藏  举报