#!/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
一、 ./if555.sh: line 22: + + + : syntax error: operand expected (error token is "+ ")
二、 运算结果:+ + +:
就是这里,有一个报错和一个技术难题。
一、报错那行的前面已经有if拦截了,怎么还会往下运行的呀?
二、第二个提示我知道是我if里的判断写错了,就是不知道如何判断前面有报错时不再显示此运算结果。