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



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