Linux shell脚本数值计算个人心得


        本篇博客为,个人对与bash数值计算的一些心得与用法。


        Bash对于数值是没有明确定义的,在shell里面所有的数值都默认为字符串,并不是单纯的数值。

        比如:a=1+2,a并不等于3,而是等于字符串1+2。


整数计算:

    

    整数计算为整数型的数值加减乘除,没有浮点数的数值,就算浮点数的数值进行加减也会忽略小数点后面的数值。

    

    表达式计算方法:

       

            表达式1:$[EXPRESSION]

            表达式2:$((EXPRESSION))

            [ ] 和 (( )) 也被称为运算符

        表达式的运算符两边可以有空格。例如: [a + b]


例子

    输入:
       a=1
       b=1
       c=2
       
       d=$[$a+$b]
       e=$(($b+$c))
       f=$[$d + $b]
       g=$(($e + $c))
       
       echo "$d"
       echo "$e"
       echo "$f"
       echo "$g"
        
    输出:
            + echo d=2
            d=2
            + echo e=3
            e=3
            + echo f=3
            f=3
            + echo g=5
            g=5

    表达式计算方式可以在运算符两边拥有空格。

       

    注意:表达式1与bash的测试条件比较相似,但是bash的测试条件是中括号两边有空格而不表达式两边。    

    测试条件:中括号两边需要空格

        [ $a>$b ]    

    表达式1:运算符两边可以有空格

        [a + b]



命令的计算方式:

        

        命令1:let

        命令2:expr

        命令3:bc




        let命令计算:

        

        格式:    let var=$a+$b

        

        let的计算只能计算整数不能计算浮点数,如果是浮点数进行计算的话,也会忽略小数后面的数字。

例如
    输入:
            a=1
            b=1
            c=2
    输出:  
            let d=$a+$b
            echo "d=$d"
            let g=$c-$b
            echo "g=$g"
            let e=$c*$c+$a
            echo "e=$e"


注意:let必须是完整的算术表达式,即有等号两边不能有空格,与上面的表达式不同,切记。





    expr计算:

    

        格式1:    var=` expr $a+$b`

        格式2:    var=$(expr $a+$b)

            $( )等同于` `(这是反引号)

            $( )与测试条件中的( )有些相似,但是测试条件中的( )是没有$符号在前面的,需要注意。

             expr的加减乘除:    乘号需要用反斜杠来进行转意

                    +

                    -

                    \*

                    /


        expr与let的区别在于expr只计算等号右边的运算表达式,然后将得出的数值复制给变量,let为将整个表达式包涵起来进行计算。不过运算能力等都一样。


        注意:expr与let有一点特别需要注意的不同是let的运算符两边不能有空格,而expr的运算符两边必须要有空格。

        

        

例如
    
    输入
            a=1
            b=1
            c=2
            
            d=`expr $a+$b`
            echo "d=$d"
            e=$(expr $a+$c)
            echo "e=$e"
            g=`expr $a*$c+$b`
            echo "g=$g"
    
    输出
            ++ expr 1 + 1
              + d=2
              + echo d=2
            d=2
            ++ expr 1 + 2
            + e=3
            + echo e=3
            e=3
            ++ expr 1 '*' 2 + 1
            + g=3
            + echo g=3
            g=3



expr与let差不多,一般用let比较多,expr的运算能力虽然和let一样,但是expr为外部组件与let内置还是有点运算数度的差别的。




浮点数计算:


        bc计算:

        

            bc为一个强大的计算工具,也是外部组件,但是bc可以进行浮点数的计算,并且可以进行进制转换


        格式:   

                echo "scale=2;$a+$b" | bc


        浮点运算,有一个很重要的概念就是精度。bc这里的精度指的的是小数的位数。在使用bc来进行除法运算的时候,你会发现默认是取整的,即没有小数部分。所以bc有个参数为scale,这个参数专门用来确定小数点后几位的位数的,需要多少位的小数,就设置为多少位。


        注意:只有除法运算的时候才会取整。


 

例子1:    在命令行进行整数加法
    
        输入
                [root@localhost ~]# echo "1+2" | bc
        输出
                3
         
         
例子2:    在命令行进行浮点数加法
        输入
                [root@localhost ~]# echo "scale=3;1.1254+2.452" | bc
        输出
                3.5774
                
例子3:    用脚本进行计算
          输入
                a=1.455
                b=1.97
                c=2.354
                
                浮点数加法
                d=`echo "$a+$b" | bc`
                echo "d=$d"
                
                浮点数乘法
                e=$(echo "$a*$b" | bc)
                echo "e=$e"
                
                浮点数除法,带scale参数
                f=`echo "scale=3;$a/$b" | bc`
                echo "f=$f"
                
                浮点数除法,不带scale参数
                g=$(echo "$a/$b" | bc)
                echo "g=$g"

        输出
                        浮点数加法输出结果
                ++ bc
                ++ echo 1.455+1.97
                + d=3.425
                + echo d=3.425
                d=3.425
                
                浮点数乘法输出结果
                ++ bc
                ++ echo '1.455*1.97'
                + e=2.866
                + echo e=2.866
                e=2.866
                
                浮点数除法,带scale参数输出结果
                ++ bc
                ++ echo 'scale=3;1.455/1.97'
                + f=.738
                + echo f=.738
                f=.738
                
                浮点数除法,不带scale参数输出结果
                ++ bc
                ++ echo 1.455/1.97
                + g=0
                + echo g=0
                g=0


        由以上的输出结果可以验证bc进行加减乘除计算的时候,只有除法是在不带scale参数的时候不会取小数位的说法。



自加自减计算:


            自加自减是比较简单的一个计算


            以上所有的计算方式都可以进行自加自减,因此这边就直接给出事例。

表达式1:   
                    输入
                            a=1
                            
                            自加1
                            a=$[$a+1]
                            echo "a++: $a"
                    输出
                            + a=2
                            + echo 'a++: 2'
                            a++: 2
                            
                            

表达式2:    
                    输入
                            c=2
                            
                            自加1,与表达式1有稍许不同。表达式2可以用++来代表+1
                            ((c++))
                            echo "c++: $c"

                    输出
                            + (( c+++ ))
                            + echo 'c++: 3'
                            c++: 3
                    
                    输入
                            c=2
                            
                            自加2,自加大于1的时候就与表达式1差不多了
                            ((c+=2))
                            echo "c++: $c"

                    输出
                            + (( c+++ ))
                            + echo 'c++: 4'
                            c++: 4
                            
                            
let计算:
                    输入
                            a=1
                            
                            自加1
                            let a++
                            echo "a++: $a"

                    输出
                            + let a++
                            + echo 'a++: 2'
                            a++: 2
                   
                   
                    输入
                            a=1
                            
                            自加2
                            let a+=2
                            echo "a+=: $a"

                    输出
                            + let a++
                            + echo 'a++: 2'
                            a++: 2
                            
                            
expr计算:
                    输入    
                            a=1
                                    
                            自加1
                              a=`expr $a + 1`
                              echo "a++: $a"


                    输出
                             ++ expr 1 + 1
                             + a=2
                              + echo 'a++: 2'
                             a++: 2

                   
                   
                    输入
                              b=1
                            
                              自加2
                             b=$(expr $b + 2)
                             echo "b+=: $b"


                    输出
                             ++ expr 1 + 2
                             + b=3
                             + echo 'b+=: 3'
                             b+=: 3
                            
                         
                                                       
bc计算:
                    输入    
                            a=1
                                    
                            自加1
                            a=`echo "$a+1" | bc`
                            echo "a++: $a"



                    输出
                             ++ bc
                               ++ echo 1+1
                             + a=2
                                    + echo 'a++: 2'
                             a++: 2


                   
                   
                    输入
                            b=1
                            
                            自加2
                            b=$(echo "$b+2" | bc)
                            echo "b+=: $b"



                    输出
                             ++ bc
                             ++ echo 1+2
                             + b=3
                             + echo 'b+=: 3'
                             b+=: 3




至此,shell脚本的计算大概已经讲的差不多了,以上为个人理解和网上的帖子学习过来的,如果有什么错误的地方希望大家能够指出,谢谢。