#!/bin/bash
#echo "1.$12.$23.$3"
If ["$" = "+"]
Then
Res= ' expr $ + $ '
echo ">>the result is: $RES"
elif ["$" = "-"]
Then
Res= ' expr $-$ '
echo ">>the result is: $RES"
#这里说明下, using "x" instead of "*"
#是因为在命令行输入 *,shell parsing problem,
#* cannot be passed to the script as an argument, so it's replaced with X,
#这样能规避这个问题, but I still don't know * the command line cannot be passed to the script,
#那位大侠能帮忙解释下, thank you very much ~ elif ["$" = "X"]
Then
res= ' Expr $ \* $
echo ">>the result is: $RES"
elif ["$" = "%"]
Then
If ["$" = "0"] # #判断除数是否为0
Then
echo "Input error,division by 0"
Exit
Fi
big= ' expr $/$ ' # #取整
Small= ' expr $ ' # #取余
Long=6 # #小数点后保留的位数
Count=0 # #循环的计数器, to be less than long
Res1=0 # #
# #无限循环, the goal is that if the small is not zero, the method of calculating the remainder is
# #在SMALL后加上LONG个0, divide by divisor, long is the exact number of digits after the decimal point
While:
Todo
Small= ' expr $SMALL \* 10 '
Count= ' expr $COUNT + 1 '
if [$COUNT = = $LONG]
Then
Break
Fi
Done
res1= ' expr $SMALL/$ ' # #到此余数就计算好了
echo ">>the result is: $BIG. $RES 1" # #把整数部分和余数部分拼接起来输出就是结果了
elif ["$" = "--help"]
Then
echo "Input format:"
echo "calculator.sh x + y"
echo "calculator.sh xy"
echo "calculator.sh x x Y"
echo "calculator.sh x y"
echo "Calculator.sh--help"
Else
echo "Input format error!"
Fi