名称:expr 
  
  ### 字串长度 
  
  shell>> expr length "this is a test" 
  14 
  
  ### 数字商数 
  
  shell>> expr 14 % 9 
  5 
  
  ### 从位置处抓取字串 
  
  shell>> expr substr "this is a test" 3 5 
  is is 
  
  ### 数字串 only the first character 
  
  shell>> expr index "testforthegame" e 
  2 
  
  ### 字串真实重现 
  
  shell>> expr quote thisisatestformela 
  thisisatestformela

~~~~~~~~~~~~~~~~~

expr命令是一个手工命令行计数器,用于在UNIX/LINUX下求表达式变量的值,一般用于整数值,也可用于字符串。
–格式为:
expr Expression(命令读入Expression 参数,计算它的值,然后将结果写入到标准输出)
–参数应用规则:
用空格隔开每个项;
用 \ (反斜杠) 放在 shell 特定的字符前面;
对包含空格和其他特殊字符的字符串要用引号括起来

–expr用法实例讲解:
(1)、计算字串长度
 > expr length “this is a test”
 14
(2)、抓取字串
 > expr substr “this is a test” 3 5
 is is
(3)、抓取第一个字符数字串出现的位置
 > expr index “sarasara”  a
 2
(4)、字串真实重现
 > expr quote sara
 sara
(5)、整数运算
 > expr 14 % 9
 5
 > expr 10 + 10
 20
 > expr 1000 + 900
 1900
 > expr 30 / 3 / 2
 5
 > expr 30 \* 3 (使用乘号时,必须用反斜线屏蔽其特定含义。因为shell可能会误解显示星号的意义)
 90
 > expr 30 * 3
 expr: Syntax error
(6)、增量计数
说明:expr在循环中用于增量计算。先将变量初始化为0,然后循环值加1,反引号的用法为命令替代。
> LOOP=0
> LOOP=`expr $LOOP + 1`
(7)、数值测试
说明:用expr测试一个数。如果试图计算非整数,则会返回错误。
> rr=3.4
> expr $rr + 1
expr: non-numeric argument
> rr=5
> expr $rr + 1
6

在expr中可以使用字符串匹配操作,这里使用模式抽取.doc文件附属名。
$expr $VALUE : ‘\(.*\).doc’
accounts

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

举例

  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。
举例
  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。
举例
  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同样能实现上述功能。
举例
  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匹配的最长字符子串。
举例
  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相配的子串。
举例
  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

linux中expr用法的更多相关文章

  1. Linux中find用法

    Linux中find用法 linux常用命令 find -name april* 在当前目录下查找以april开始的文件 find -name april* fprint file 在当前目录下查找以 ...

  2. linux中nl用法

    linux 中nl 命令使用 nl :添加行号打印 -b:   指定行号指定的方式,主要有两种:    -b a : 表示不论是否为空行,都同样列出行号    -b t : 如果有空行,则不列出那一行 ...

  3. linux中read用法

    read在while中的经常用法: root@ubuntu:/var/lib/logrotate :: # cat /etc/cron.daily/logrotate #!/bin/sh # Clea ...

  4. linux中sed用法

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed ...

  5. Linux中wget用法

    Wget简介:Linux系统中wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTP ...

  6. linux中convert用法

    转: 强大的convert命令 convert命令可以用来转换图像的格式,支持JPG, BMP, PCX, GIF, PNG, TIFF, XPM和XWD等类型,下面举几个例子:   convert  ...

  7. linux中xargs用法

    参数代换: xargs xargs 是在做什么的呢?就以字面上的意义来看, x 是加减乘除的乘号,args 则是 arguments (参数) 的意思,所以说,这个玩意儿就是在产生某个命令的参数的意思 ...

  8. cut,sort,awk,sed,tr,find,wc,uniq在Linux中的用法

    cut语法cut [-bn] [file]cut [-c] [file]cut [-df] [file] -b :以字节为单位进行分割.这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志.-c ...

  9. linux中fuser用法详解

    fuser功能 fuser 可以显示出当前哪个程序在使用磁盘上的某个文件.挂载点.甚至网络端口,并给出程序进程的详细信息.  fuser显示使用指定文件或者文件系统的进程ID.默认情况下每个文件名后面 ...

随机推荐

  1. PAT——乙级1008

    1008 数组元素循环右移问题 (20 point(s)) 一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A​0​​A​ ...

  2. c# 钩子程序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  3. HDU5726 GCD

    Give you a sequence of N(N≤100,000)N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000)a1,...,an( ...

  4. 浅谈JavaScript中的函数问题

    前面的话:JavaScript可运行在所有主要平台的主流浏览器上,也可运行在每一个主流操作系统的服务器端上.所以呢,要想成为一名优秀的全栈工程师,必须懂得JavaScript语言.这是我整理的JS的部 ...

  5. 【转】Apache Thrift - 可伸缩的跨语言服务开发框架

    Apache Thrift - 可伸缩的跨语言服务开发框架 Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Java 开发人员角度详 ...

  6. mac最新系统安装beego出现kiil 9

    (内容来自:http://www.oschina.net/question/2626413_2237311) 应该是最新mac OS 12.04的锅. 现在的解决办法是回退bee到以前版本. cd $ ...

  7. Linux常用命令与基本概念

    复制 文件的复制 cp 源文件 目标文件 文件夹的复制 cp -r 源文件夹 目标文件夹 删除 删除文件 rm 文件名 删除文件夹 rm -rf 文件夹 查看文件类型 file 文件名 压缩与解压 z ...

  8. POJ 2836:Rectangular Covering(状态压缩DP)

    题目大意:在一个平面内有若干个点,要求用一些矩形覆盖它们,一个矩形至少覆盖两个点,可以相互重叠,求矩形最小总面积. 分析: 数据很小,很容易想到状压DP,我们把点是否被覆盖用0,1表示然后放在一起得到 ...

  9. 论S B的自我修养 【2015/10/18更】

    to do list: 1.正则表达式引擎   (done 2.五子棋AI jquery && canvas 游戏 (这个搞定好多好玩的idea可以实现了php 暂时不想玩各种框架吧, ...

  10. Docker 开源项目之 registry - 部署 registry (注册表)服务器

    原文地址 在部署 registry 之前需要现在主机上安装 Docker.registry 实际上就是运行在 Docker 中的 registry 镜像的实例. 本主题提供关于部署和配置 regist ...