bash shell中expr命令下几种的使用

expr在linux中是一个功能非常强大的命令。通过学习做一个小小的总结。
1、计算字符串的长度。我们可以用awk中的length(s)进行计算。我们也可以用echo中的echo ${#string}进行计算,当然也可以expr中的expr length $string 求出字符串的长度。

举例

[plain]  view plain copy
  1. [root@localhost shell]# string="hello,everyone my name is xiaoming"  
  2. [root@localhost shell]# echo ${#string}  
  3. 34  
  4. [root@localhost shell]# expr length "$string"  
  5. 34  

2、expr中的expr index $string substring索引命令功能在字符串$string上找出substring中字符第一次出现的位置,若找不到则expr index返回0或1。
举例
[python]  view plain copy
  1. [root@localhost shell]# string="hello,everyone my name is xiaoming"  
  2. [root@localhost shell]# expr index "$string" my  
  3. 11  
  4. [root@localhost shell]# expr index "$string" nihao  
  5. 1  
3、expr中的expr match $string substring命令在string字符串中匹配substring字符串,然后返回匹配到的substring字符串的长度,若找不到则返回0。
举例
[python]  view plain copy
  1. [root@localhost shell]# string="hello,everyone my name is xiaoming"  
  2. [root@localhost shell]# expr match "$string" my  
  3. 0  
  4. [root@localhost shell]# expr match "$string" hell.*  
  5. 34  
  6. [root@localhost shell]# expr match "$string" hell  
  7. 4  
  8. [root@localhost shell]# expr match "$string" small  
  9. 0  
4、在shell中可以用{string:position}和{string:position:length}进行对string字符串中字符的抽取。第一种是从position位置开始抽取直到字符串结束,第二种是从position位置开始抽取长度为length的子串。而用expr中的expr substr $string $position $length同样能实现上述功能。
举例
[python]  view plain copy
  1. root@localhost shell]# string="hello,everyone my name is xiaoming"  
  2. [root@localhost shell]# echo ${string:10}  
  3. yone my name is xiaoming  
  4. [root@localhost shell]# echo ${string:10:5}  
  5. yone  
  6. [root@localhost shell]# echo ${string:10:10}  
  7. yone my na  
  8. [root@localhost shell]# expr substr "$string" 10 5  
  9. ryone  
注意:echo ${string:10:5}和 expr substr "$string" 10 5的区别在于${string:10:5}以0开始标号而expr substr "$string" 10 5以1开始标号。
5、删除字符串和抽取字符串相似${string#substring}为删除string开头处与substring匹配的最短字符子串,而${string##substring}为删除string开头处与substring匹配的最长字符子串。
举例
[python]  view plain copy
  1. [root@localhost shell]# string="20091111 readnow please"  
  2. [root@localhost shell]# echo ${string#2*1}  
  3. 111 readnow please  
  4. [root@localhost shell]# string="20091111 readnow please"  
  5. [root@localhost shell]# echo ${string##2*1}  
  6. readnow please  
解析:第一个为删除2和1之间最短匹配,第二个为删除2和1之间的最长匹配。
6、替换子串${string/substring/replacement}表示仅替换一次substring相配字符,而${string//substring//replacement}表示为替换所有的substring相配的子串。
举例
[python]  view plain copy
  1. [root@localhost shell]# string="you and you with me"  
  2. [root@localhost shell]# echo ${string/you/me}  
  3. me and you with me  
  4. [root@localhost shell]# string="you and you with me"  
  5. [root@localhost shell]# echo ${string//you/me}  
  6. me and me with me  




expr 命令


用途

求表达式变量的值。

语法

expr Expression

描述

expr 命令读入 Expression 参数,计算它的值,然后将结果写入到标准输出。

您必须对 Expression 参数应用以下规则:

用空格隔开每个项。
用 / (反斜杠) 放在 shell 特定的字符前面。
对包含空格和其他特殊字符的字符串要用引号括起来。
整数前面可以放一个一元连字符。在内部,整数被当作 32 位,双互补数。


注:expr 命令返回一个 0 来指示一个零值,而不是空字符串。
以下项描述了 Expression 参数的运算符和关键字。需要转义的字符由一个 / (反斜杠) 放在前面。这些项按照优先权递增的顺序列出,具有相等的优先权的运算符分组在 {} (大括号) 中:

Expression1 /| Expression2 返回 Expression1如果它不是空值或者 0 值的话,否则返回 Expression2。
Expression1 /& Expression2 返回 Expression1如果两个表达式都不是空值或者 0 值,否则返回一个 0 值。
Expression1 { =, />, />=, /<, /<=, != } Expression2 如果两个表达式都是整数,返回整数比较的结果;否则它返回的是字符串比较的结果。
Expression1 {+, - } Expression2 整数值变量的加或者减。
Expression1 { /*, /, % } Expression2 整数值变量的乘、除或者提供除法的余数。
Expression1 : Expression2 将 Expression1 的运算得到的字符串与 Expression2 的运算结果的正则表达式模式进行比较。正则表达式语法与 ed 命令相同,除了所有的模式固定到字符串的开始之外(也就是说,只有以字符串的第一个字符开始的序列才被正则表达式匹配)。因此 ^ (插入符号) 在这种情况下就不是特殊字符。
一般地,匹配运算符返回匹配的字符个数(失败的时候返回 0)。如果模式包含了一个子表达式,也就是:


/( Expression /)
则会返回包含了实际匹配的字符的字符串。

整理顺序可以定义在字符范围内使用的等价类。有关整理顺序和等价类的更多信息,请参阅《AIX V6.1 本地语言支持指南与参考大全》中的『了解语言环境的环境变量』。


注: 以下字符串变量会超过标准,其行为可能在不同的操作系统中会有不同。这些字符串变量是“不”可移植的。
match String1 String2 与 Expression1 : Expression2 相同。
length String1 返回 String1 的长度。
index String1 String2 返回 String1 中包含 String2 中任意字符的第一个位置。
substr String1 StartPosition Length
                          返回一个以 StartPosition 的字符开始的在 String1 中的字符串,并且是 Length 长度的字符串。
退出状态

此命令返回以下退出值:

0 Expression 参数运算值既不为空也不为 0。
1 Expression 参数的计算值或者为空或者为 0。
2 Expression 参数无效。
>2 发生错误。

注:在 shell 处理完参数后,expr 命令除了利用数值外不能区分运算符和操作数。因此,如果 $a 的值是 j 的话,命令:

expr $a = j
就像:


expr j = j
在 shell 将所有的参数都传给 expr 命令之后。以下情况也为 true:


expr X$a = Xj
示例

要修改一个 shell 变量,请输入:
COUNT=`expr $COUNT + 1`
这会加 1 到 shell 变量 $COUNT 上。 expr 命令以 grave accent 包装,这会导致 shell 将 expr 命


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值