shell編程

shell編程
Shell 腳本中的數(shù)學(xué)運(yùn)算
bash shell 的基礎(chǔ)運(yùn)算主要有4種形式:
- $(( expression ))
- $[ expression ]
- expr operation
- 使用 let
實(shí)例:
#!/bin/bash
set -e
set -u
a=20
echo "a = $a"
b=6
echo "b = $b"
c=4.5
echo "c = $c"
result=$(( a + b ))
echo "The result of '\$(( a + b ))' is $result"
result1=$[ a - b ]
echo "The result of '\$[ a - b ]' is $result1"
result2=`expr $a \* $b`
echo "The result of '\`expr \$a \* \$b\`' is $result2"
let "result3 = $a + $b"
echo "The result of 'let \"result3 = \$a + \$b\"' (result3) is $result3"
result4=$(expr $a / $b )
echo "The result of '\$(expr \$a / \$b )' is $result4"
result5=$(( a + c ))
結(jié)果:
#!/bin/bash
set -e
set -u
a=20
echo "a = $a"
b=6
echo "b = $b"
c=4.5
echo "c = $c"
result=$(( a + b ))
echo "The result of '\$(( a + b ))' is $result"
result1=$[ a - b ]
echo "The result of '\$[ a - b ]' is $result1"
result2=`expr $a \* $b`
echo "The result of '\`expr \$a \* \$b\`' is $result2"
let "result3 = $a + $b"
echo "The result of 'let \"result3 = \$a + \$b\"' (result3) is $result3"
result4=$(expr $a / $b )
echo "The result of '\$(expr \$a / \$b )' is $result4"
result5=$(( a + c ))
推薦涉及運(yùn)算,統(tǒng)一使用,方便區(qū)分
$[ expression ]