#!/bin/bash
while true
do
read -p "please input one equation: " x f y
help(){
echo "Usage: [number] [+-*/%] [number]..."
continue
}
#判断是否需要帮助
if [ "$x" = "help" ] || [[ "$x" = "?" ]] || [[ "$x" = "h" ]]; then
help
fi
#判断是否为数字
num1=`echo $x|sed 's/[0-9]//g'|sed 's/[.]//'`
if [ ! -z $num1 ]; then
echo "first num error,please input num!"
help
fi
num2=`echo $y|sed 's/[0-9]//g'|sed 's/[.]//'`
if [ ! -z $num2 ]; then
echo "second num error,please input num!"
help
fi
#因为乘法符号的特殊性,所以先判断是否为乘法,如果不是乘法再判断是否为其他的运算符
if [ "$f" == "*" ]; then
echo -n "a * b = " ;echo $x$f$y|bc
continue
fi
#判断是否为 + - / % 运算符
opt=`echo \$f|sed 's/[+,/,%,-]//g'`
if [ ! -z $opt ]; then
echo "please input a right operational character!"
help
fi
#echo "a" $f "b =" $(($x$f$y)) #对部分运算符不支持,bc可以支持小数运算
echo -n "a ";echo -n $f;echo -n " b = ";echo $x$f$y|bc
done