码迷,mamicode.com
首页 > 其他好文 > 详细

expr命令

时间:2014-08-08 01:38:45      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   os   strong   ar   div   

expr命令的兩大作用:
1)四则运算;
2)字符串的操作;

1、四则运算
[tough@localhost ~]$ expr 6+2
6+2

[tough@localhost ~]$ expr 6 + 2
8

[tough@localhost ~]$ expr 6 - 2
4

[tough@localhost ~]$ expr 6 / 2
3

[tough@localhost ~]$ expr 6 * 2
expr: syntax error

[tough@localhost ~]$ expr 6 \* 2
12

[tough@localhost ~]$ expr 6 + 3 - 5
4

注意: 运算符左右都有空格 ,如果没有空格表示是字符串连接
使用乘号时,必须用反斜线屏蔽其特定含义。因为shell可能会误解显示星号的意义。

[tough@localhost ~]$ expr 1.4 / 2
expr: non-numeric argument

[tough@localhost ~]$ echo $?
2

[tough@localhost ~]$ expr 2 / 2 
1

[tough@localhost ~]$ echo $?  
0
计算非整数,将返回错误,且返回碼非0。


2、字符串的操作
1)截取子串
从第7位开始,截取5位。
[tough@localhost ~]$ expr substr "Hello World" 7 5
World

 

2)提取指定字符的下标
在"123#45"中找到"#"的位置,在第4位。
[tough@localhost ~]$ expr index "123#45" "#"
4

 

expr命令,布布扣,bubuko.com

expr命令

标签:style   blog   color   使用   os   strong   ar   div   

原文地址:http://www.cnblogs.com/toughhou/p/3898409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!