zoukankan      html  css  js  c++  java
  • 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
    (8)、模式匹配
    说明:expr也有模式匹配功能。可以使用expr通过指定冒号选项计算字符串中字符数。.*意即任何字符重复0次或多次。
    > VALUE=account.doc
    > expr $VALUE : ‘.*’
    8
    在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  
  • 相关阅读:
    每日日报24
    每日日报23
    每日日报22
    链路层:MAC 地址
    应用层:电子邮件
    应用层:HTTP 协议
    应用层:DNS 域名系统
    运输层:TCP 拥塞控制
    运输层:拥塞控制原理
    JAVA学习日记26-0731
  • 原文地址:https://www.cnblogs.com/yi-meng/p/3194406.html
Copyright © 2011-2022 走看看