shell 的數字計算

# 測試shell腳本中的數字運算
add(){
echo 'start func'
echo 'first parameter is:'$1
echo 'second parameter is :'$2
result1=$(expr $1 + $2)
result2=$(expr $result1 / 3)
result3=$(expr $result2 /* 2)  #注意*乘法的字符轉換,要加轉義字符
echo "result1 is" $result1
echo "result2 is" $result2
echo "result3 is" $result3
}

ls
echo $$
echo $?
test $? -eq 0 && echo 'ok'


add 3 4 #call  add function


n=40

result4=`echo "scale=5;s($n)"| bc -l`  #用反引號把計算結果給result變量,調用sin函數,讓bc函數使用數學庫
result4=$(echo "scale=5;s($n)"| bc -l)  #用變量括號也是一種方法
result5=$(expr $n /* 5)
result6=$(echo "sacel=5;$n*2.2" |bc -l)
echo     "result4 is:" $result4
echo     "result5 is:" $result5
echo     "result6 is:" $result6



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章