zoukankan      html  css  js  c++  java
  • expr命令

    expr命令的兩大作用:
    1)四则运算;
    2)字符串的操作;

    1、四则运算
    [tough@localhost ~]$ expr 6+2
    6+2
    
    [tough@localhost ~]$ expr 6 + 2
    8
    
    [tough@localhost ~]$ expr 6 - 2
    4
    
    [tough@localhost ~]$ expr 6 / 2
    3
    
    [tough@localhost ~]$ expr 6 * 2
    expr: syntax error
    
    [tough@localhost ~]$ expr 6 * 2
    12
    
    [tough@localhost ~]$ expr 6 + 3 - 5
    4

    注意: 运算符左右都有空格 ,如果没有空格表示是字符串连接
    使用乘号时,必须用反斜线屏蔽其特定含义。因为shell可能会误解显示星号的意义。

    [tough@localhost ~]$ expr 1.4 / 2
    expr: non-numeric argument
    
    [tough@localhost ~]$ echo $?
    2
    
    [tough@localhost ~]$ expr 2 / 2 
    1
    
    [tough@localhost ~]$ echo $?  
    0
    计算非整数,将返回错误,且返回碼非0。


    2、字符串的操作
    1)截取子串
    从第7位开始,截取5位。
    [tough@localhost ~]$ expr substr "Hello World" 7 5
    World
    2)提取指定字符的下标
    在"123#45"中找到"#"的位置,在第4位。
    [tough@localhost ~]$ expr index "123#45" "#"
    4
  • 相关阅读:
    spring1冲刺感想与总结
    Scrum 项目7.0
    读《构造之法》8、9、10章有感
    Scrum4.0+5.0
    scrum3.0
    0512操作系统之进程调度
    Scrum 项目1.0
    团队之阅读感想
    sprint3终极演示
    0622—软件工程第一学期总结
  • 原文地址:https://www.cnblogs.com/toughhou/p/3898409.html
Copyright © 2011-2022 走看看