数学表达式(expr和bc)

#!/bin/sh
c=5
d=6
#加
answer=`expr $c + $d`
echo $answer
#减
answer=`expr $c - $d`
echo $answer
#乘
answer=`expr $c \* $d`
echo $answer
#除
answer=`expr $c / $d`
echo $answer
#同余
answer=`expr $c % $d`
echo $answer
#加
answer=$(($c + $d))
echo $answer
#减
answer=$(($c - $d))
echo $answer
#乘
answer=$(($c * $d))
echo $answer
#除
answer=$(($c / $d))
echo $answer
#同余
answer=$(($c % $d))
echo $answer
#指数运算
answer=$(($c ** $d))
echo $answer
#复杂的运算
answer=$((($c+$d)*$c))

echo $answer

------------------------------------------------------

#!/bin/sh
c=10
d=15
#加法
answer=`echo "$c+$d" |bc -l`
echo $answer
#减法
answer=`echo "$c-$d" |bc -l`
echo $answer
#乘法
answer=`echo "$c*$d" |bc -l`
echo $answer
#除法
answer=`echo "$c/$d" |bc -l`
echo $answer
#余数
answer=`echo "$c%$d" |bc -l`
echo $answer
#指数运算
answer=`echo "$c^$d" |bc -l`
echo $answer

你可能感兴趣的:(Shell)