运算符两种方式
方式一:expr $num1 operator $num2
方式二:$(($num1 operator $num2)) (方式二在运算符“=”时候会出错)
1+2.sh
这个代码有点问题,尚没有解决
while true do read -p "please input a positive number:" num expr $num + 1 &> /dev/null if [ $? -eq 0 ];then if [ `expr $num > 0` -eq 1 ];then for((i=1;i<=$num;i++)) do sum=`expr $num + $i` done echo "1+2+3+...+$num = $sum" exit fi fi echo "error,input enlegal" continue done
bc.sh
脚本中运用bc命令
#!/bin/bash # read -p "num1:" num1 read -p "num2:" num2 echo "scale=3;$num1/$num2" | bc