zoukankan      html  css  js  c++  java
  • shell中的算数运算

    * 括号的用法

    算数运算的表示


    1.expr

    cmdshell
    expr 3 + 3 res=`expr 3 + 3`

    expr 3 - 7

    res=`expr 3 - 7`
    expr 17 / 8 res=`expr 17 / 8`
    expr 17 *  3 res=`expr 17 *  3`
    expr ( 17 + 3 ) * 2 / 4 res=`expr ( 17 + 3 ) * 2 / 4`

      使用运算符时,一定要注意运算符左右两边要留有空格。shell中使用“反引号”把表达式括起来。乘法( * )运算、小括号需要使用“转义字符”。

    2.$(( ))

      该运算无需对运算符和小括号使用转义字符;也不需要空格。

    cmd

    shell

    echo $((3+3)) res=$((3+3))
    echo $((3-3)) res=$((3-3))
    echo $((3*3)) res=$((3*3))
    echo $((3/3)) res=$((3/3))
    echo $((3*3+4*4))

    res=$((3*3+4*4))

    echo $(((3+2)*7)) res=$(((3+2)*7)))
    [root@right mag]# echo $((3+3))
    6
    [root@right mag]# echo $((3*3+4*4))
    25

    3.$[ ]

      “同上”。

    [root@right mag]# echo $[3+3]
    6
    [root@right mag]# echo $[3*3+4*4]
    25

    4.let

      使用let时,变量名不必使用$符号。

    cmdshell
    let score=80+7 && echo $score let score=80+7
    let score=80-7 && echo $score let score=80-7
    let score=80*7 && echo $score let score=80*7
    let score=80/7 && echo $score let score=80/7
    let score=(20+60)*2 && echo $score let score=(20+60)*2
    let score=(60-4)/7 && echo $score let score=(60-4)/7
    * 如果符号之间有空格,需要在表达式两边加上双引号
    [root@right mag]# let score=80+7 && echo $score
    87
    [root@right mag]# let score=(20+60)*2 && echo $score
    160
    一切代码都是为了生活,一切生活都是调剂
  • 相关阅读:
    在线工具TOOL收藏
    HtmlDocument [代码碎片笔记]
    ChromiumWebBrowser [链接]
    PHP [开发汇总]
    Discuz[技术文献]
    [jvm] -- 监控和调优常用命令工具篇
    [jvm] -- 常用内存参数配置篇
    [日常摘要] -- 事务的隔离级别篇
    [日常摘要] -- ThreadLocal篇
    [日常摘要] -- zookeeper篇
  • 原文地址:https://www.cnblogs.com/argor/p/7909185.html
Copyright © 2011-2022 走看看