expr命令

expr命令,功能多多呀,常规的作用就是做运算。还有模式匹配,计算长度,判断是不是数值,计算字符串长度等功能。

$ echo $LINK
tools/apache-tomcat-6.0.37.tar.gz
$ expr "$LINK" : '.*/.*'          
34
$ expr "$LINK" : '\(.*\)/.*' 
 tools


http://linux.101hacks.com/unix/expr/

Linux 101 Hacks


4 UNIX / Linux expr Command Examples

by BALA

What is expr?
4 expr examples
Syntax and Options
Related Commands

anchor.gif

What is expr?

expr evaluates the given expression.


anchor.gif

4 expr Examples

1. Perform string matching operations

expr command helps us to perform different levels of string matching operations with the operator ‘:’ as shown below,

# partial match and returns the number of characters matched.
$ expr linux : lin
3

# the condition is string 2 entirely should match in string 1.
$ expr linux : linx
0

# regular expression to match any number of characters
$ expr linux : '.*'
5

# to print the matched characters instead of number of matching positions
$ expr linux : '..\(...\)'
nux

Note : expr command should have the space between the operator and operands.

2. Compare the two expressions

Using expr command, you can compare two expressions (numbers or strings). It returns either 0 for failure or 1 for success as shown below

$ var1='10'
$ var2='20'

# matching numbers with '='.
$ expr $var1 = $var2
0

# displays 1 when arg1 is less than arg2
$ expr $var1 \< $var2
1

# display 1 when arg1 is not equal to arg2
$ expr $var1 \!= $var2
1

3. Perform the integer arithmetic operations

You can do the integer arithmetic operations like addition, subtraction, multiplication, division and modulus. In the below example, two numbers are multiplied and the result is produced as follows.

$ expr 5 \* 2
10

4. Increment the value inside the script

The example below increments the $count variable value to 1 inside the shell script.

echo $count
count=`expr $count + 1`

Syntax and Options

expr EXPRESSION
expr OPTION

Short Option Long Option Option Description
–help to display help page and exit
–version to display version information and exit
ARG1 * ARG2 Interger arithmetic operator for multiplication
ARG1 / ARG2 Interger arithmetic operator for division
ARG1 + ARG2 Interger arithmetic operator for addition
ARG1 – ARG2 Interger arithmetic operator for substraction
ARG1 % ARG2 Interger arithmetic operator for modulus operation
ARG1 = ARG2 Comparision operator to check both expressions are equals
ARG1 < ARG2 Comparision operator to check arg1 is less than arg2
ARG1 <= ARG2 Comparision operator to check arg1 is lesser than or equal to arg2
ARG1 > ARG2 Comparision operator to check arg1 is greater than arg2
ARG1 >= ARG2 Comparision operator to check arg1 is greater than or equal to arg2
ARG1 != ARG2 Comparision operator to check arg1 is not equal to arg2
ARG1 : ARG2 String matching operator
ARG1 & ARG2 Conditional operator which returns arg1 when arg1 and arg2 are not 0 or NUL. otherwise 0.
ARG1 | ARG2 Conditional operator which returns arg1 if it not 0 or NUL otherwise arg2 is returned.

转载于:https://my.oschina.net/swuly302/blog/205193

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值