linux中expr用法
名称: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 求出字符串的长度。
举例
- [root@localhost shell]# string="hello,everyone my name is xiaoming"
- [root@localhost shell]# echo ${#string}
- 34
- [root@localhost shell]# expr length "$string"
- 34
- [root@localhost shell]# string="hello,everyone my name is xiaoming"
- [root@localhost shell]# expr index "$string" my
- 11
- [root@localhost shell]# expr index "$string" nihao
- 1
- [root@localhost shell]# string="hello,everyone my name is xiaoming"
- [root@localhost shell]# expr match "$string" my
- 0
- [root@localhost shell]# expr match "$string" hell.*
- 34
- [root@localhost shell]# expr match "$string" hell
- 4
- [root@localhost shell]# expr match "$string" small
- 0
- root@localhost shell]# string="hello,everyone my name is xiaoming"
- [root@localhost shell]# echo ${string:10}
- yone my name is xiaoming
- [root@localhost shell]# echo ${string:10:5}
- yone
- [root@localhost shell]# echo ${string:10:10}
- yone my na
- [root@localhost shell]# expr substr "$string" 10 5
- ryone
注意:echo ${string:10:5}和 expr substr "$string" 10 5的区别在于${string:10:5}以0开始标号而expr substr "$string" 10 5以1开始标号。
- [root@localhost shell]# string="20091111 readnow please"
- [root@localhost shell]# echo ${string#2*1}
- 111 readnow please
- [root@localhost shell]# string="20091111 readnow please"
- [root@localhost shell]# echo ${string##2*1}
- readnow please
- [root@localhost shell]# string="you and you with me"
- [root@localhost shell]# echo ${string/you/me}
- me and you with me
- [root@localhost shell]# string="you and you with me"
- [root@localhost shell]# echo ${string//you/me}
- me and me with me
linux中expr用法的更多相关文章
- Linux中find用法
Linux中find用法 linux常用命令 find -name april* 在当前目录下查找以april开始的文件 find -name april* fprint file 在当前目录下查找以 ...
- linux中nl用法
linux 中nl 命令使用 nl :添加行号打印 -b: 指定行号指定的方式,主要有两种: -b a : 表示不论是否为空行,都同样列出行号 -b t : 如果有空行,则不列出那一行 ...
- linux中read用法
read在while中的经常用法: root@ubuntu:/var/lib/logrotate :: # cat /etc/cron.daily/logrotate #!/bin/sh # Clea ...
- linux中sed用法
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为: sed ...
- Linux中wget用法
Wget简介:Linux系统中wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTP ...
- linux中convert用法
转: 强大的convert命令 convert命令可以用来转换图像的格式,支持JPG, BMP, PCX, GIF, PNG, TIFF, XPM和XWD等类型,下面举几个例子: convert ...
- linux中xargs用法
参数代换: xargs xargs 是在做什么的呢?就以字面上的意义来看, x 是加减乘除的乘号,args 则是 arguments (参数) 的意思,所以说,这个玩意儿就是在产生某个命令的参数的意思 ...
- cut,sort,awk,sed,tr,find,wc,uniq在Linux中的用法
cut语法cut [-bn] [file]cut [-c] [file]cut [-df] [file] -b :以字节为单位进行分割.这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志.-c ...
- linux中fuser用法详解
fuser功能 fuser 可以显示出当前哪个程序在使用磁盘上的某个文件.挂载点.甚至网络端口,并给出程序进程的详细信息. fuser显示使用指定文件或者文件系统的进程ID.默认情况下每个文件名后面 ...
随机推荐
- 聊聊、Java 命令 第二篇
第一篇类之间没有依赖关系,所以比较简单,这一篇来看看有依赖的类怎么编译和执行. (一)Java 运行 class 有依赖 Person 是一个接口,只有一个 getName 方法.Man 则实现了 P ...
- FastDFS的安装(复制自己用)
FastDFS 安装及使用 FastDFS 安装及使用 2012-11-17 13:10:31| 分类: Linux|举报|字号 订阅 Google了一下,流行的开源分布式文件系统有很多,介 ...
- hdu5985[概率dp] 2016青岛icpc现场赛
#include <bits/stdc++.h> using namespace std; ][]; ][]; ][]; ]; ]; int T, n; double fastpow(do ...
- 【bzoj3295】[Cqoi2011]动态逆序对 线段树套SBT
题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序 ...
- HDU 3987 Harry Potter and the Forbidden Forest(边权放大法+最小割)
Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/ ...
- .config 中特殊字符的处理
我们知道在应用程序中嵌入连接字符串可能导致安全漏洞和维护问题.使用 Ildasm.exe(MSIL 反汇编程序) 工具可以查看编译到应用程序源代码中的未加密连接字符串.此外,如果连接字符串发生更改,则 ...
- js 一/二维数组排序
JavaScript中数组排序方法 用到的最多的当然是封装好的sort()方法了 一:sort()方法怎么使用? sort方法并不像我们想的那么容易使用,不是单纯的arr.sort()就行了,需要我们 ...
- nodeJS(2)深了解: nodeJS 项目架构详解(app.js + Express + Http)
简略了解:nodeJS 深了解(1): Node.js + Express 构建网站预备知识 环境: 环境: win7 + nodeJS 版本(node): 新建 nodeJS 项目: 名称为: te ...
- XWW的难题(bzoj 3698)
Description XWW是个影响力很大的人,他有很多的追随者.这些追随者都想要加入XWW教成为XWW的教徒.但是这并不容易,需要通过XWW的考核.XWW给你出了这么一个难题:XWW给你一个N*N ...
- 【HDOJ5533】Dancing Stars on Me(计算几何)
题意:给定二维平面上的n个整点,问它们是否都在正n边形的定点上 n<=100,abs(x[i]),abs(y[i])<=1e4 思路:队友做的,抱大腿 可以发现只有n=4时顶点有可能都是整 ...