脚本如下
#!/bin/bash
read -t 30 -p "请输入数值1:" num1
read -t 30 -p "请输入数值2:" num2
read -t 30 -p "请输入运算符:" ope
if [ -n "$num1" -a -n "$num2" -a -n "$ope" ]
then
test1=$(echo $num1 | sed 's/[0-9]//g')
test2=$(echo $num2 | sed 's/[0-9]//g')
if [ -z "$test1" -a -z "$test2" ]
then
if [ "$ope" == '+' ]
then
sum = $(($num1+$num2))
elif [ "$ope" == '-' ]
then
sum = $(($num1-$num2))
elif [ "$ope" == '*' ]
then
sum = $(($num1*$num2))
elif [ "$ope" == '/' ]
then
sum = $(($num1/$num2))
else
echo "只支持加减乘除四种运算符!"
exit 10
fi
else
echo "必须输入数字!"
exit 11
fi
else
echo "输入的数值或运算符不能为空!"
exit 12
fi
echo "$num1 $ope $num2 = $sum"
sum = $(($num1+$num2)),=前后不能有空格吧,得是sum=$(($num1+$num2))