老師交待的作業,不用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
2018-03-31
一、 ./if555.sh: line 22: + + + : syntax error: operand expected (error token is "+ ")
二、 運算結果:+ + +:
就是這里,有一個報錯和一個技術難題。
一、報錯那行的前面已經有if攔截了,怎么還會往下運行的呀?
二、第二個提示我知道是我if里的判斷寫錯了,就是不知道如何判斷前面有報錯時不再顯示此運算結果。