背景:
阅读新闻
Shell编程简单实现四则运算
[日期:2012-07-21] 来源:Linux社区 作者:lj627889343 [字体:]
Shell编程简单实现四则运算:
#!/bin/sh main() { echo "#############################" echo "## usage: num1 +/-/#// num2##" echo "## example: 1 + 1 ##" echo "## '*' is replaced by '#' ##" echo "#############################" while [ 1 ] do echo "## please select : ##" echo "## 1.cal ##" echo "## 0.exit ##" read select if [ "$select" = "1" ] then cal; elif [ "$select" = "0" ] then break; fi done } cal() { echo "please enter number,as: number1 sign number2:" read str num1=`echo $str|cut -d' ' -f1` num2=`echo $str|cut -d' ' -f3` sign=`echo $str|cut -d' ' -f2` case $sign in +) sum=`expr $num1 + $num2`; echo "$num1 $sign $num2 = $sum";; -) sum=`expr $num1 - $num2`; echo "$num1 $sign $num2 = $sum";; \#) sum=`expr $num1 \* $num2`; echo "$num1 * $num2 = $sum";; /) sum=`expr $num1 / $num2`; echo "$num1 $sign $num2 = $sum";; *) echo "your enter is wrong";; esac echo "please enter to continue" read cto } main