Linux bash in operation

  1. First, the use of ((expression))

    root@root:~# a=3
    root@root:~# ((b=a+3))
    root@root:~# echo $b
    6

  2. The second use let

    root@root:~# let "c=$a+4"
    root@root:~# echo $c
    7

  3. Use expr expression, note the space and backtick

    root@root:~# d=`expr 4 + 3`
    root@root:~# echo $d
    7

  4. On $ [expression], which is bash the recommended standard treatment

    root@root:~# a=4
    root@root:~# b=$[$a-9]
    root@root:~# echo $b
    -5

  5. In the for-loop style C, may be used as follows

    root@root:~# for((a=1,b=10;a<=10;a++,b--))
    do
    ((c=a*b))
    echo "$c"
    done
    10
    18
    24
    28
    30
    30
    28
    24
    18
    10

  6. Floating-point operations, with bc (bash calculator) operation

    基本格式: variable=`echo "option;expression" | bc`
    root@root:~# a=`echo "scale=4;3.25/3" | bc`
    root@root:~# echo $a
    1.0833
    root@root:~# a=6.6
    root@root:~# b=3.5
    root@root:~# c=`echo "scale=5;$a/$b"|bc`
    root@root:~# echo $c
    1.88571

  7. If a floating-point expression is more than one line, you can put multiple lines computing

    基本格式:
    variable=`bc << EOF
    options
    statements
    expressions
    EOF
    `
    演示:
    root@root:~# a=1.2
    root@root:~# b=2.4
    root@root:~# c=3.5
    root@root:~# d=2
    root@root:~# e=`bc << EOF
    scale=4
    ab=($a+$b)
    cd=($c-$d)
    ab+cd
    EOF
    `
    root@root:~# echo $e
    5.1

Guess you like

Origin www.cnblogs.com/xhai/p/11369936.html