老师交待的作业,不用if嵌套写了个加减乘除,大神们有BUG吗?
#!/bin/bash
read -p "Please input num1:" num1
read -p "Please input num2:" num2
read -p "Please input operator:" operator
if [ -z "$num1" -o -z "$operator" -o -z "$num2" ]
then
echo "输入数值有空值,请重新输入"
exit 10
fi
test1=$( echo $num1 | sed 's/[0-9]//g' )
test2=$( echo $num2 | sed 's/[0-9]//g' )
if [ -n "$test1" -o -n "$test2" ]
then
echo "请输入数值"
exit 11
fi
if [ "$operator" == '+' ]
then
sum=$(( $num1 + $num2 ))
elif [ "$operator" == '-' ]
then
diff=$(( $num1 - $num2 ))
elif [ "$operator" == '*' ]
then
pro=$(( $num1 * $num2 ))
elif [ "$operator" == '/' ]
then
quo=$(( $num1 / $num2 ))
else
echo "请输入正确的运算符"
exit 12
fi
echo "运算结果:$num1 $operator $num2:"$sum$diff$pro$quo