如果運算符輸入為空,為什么會返回第一層判斷失敗的錯誤?
#!/bin/bash
read -t 30 -p "請輸入一個數字:" num1
read -t 30 -p "請輸入一個數字:" num2
read -t 30 -p "請輸入一個運算符:" ope
#通過read命令接受要輸入的參數
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')
#定義變量test1和test2的值。把變量test值替換為空。如果能替換
#則為數字
? ? ? ? if [ -z "$test1" -a -z "$test2" ]
#判斷變量test1 2是否為空,為空證明 num1 num2是數字
? ? ? ? ? ? ? ? then
? ? ? ? ? ? ? ? ? ? ? ? if [ -n -o "$ope" == '+' -o "$ope" == '-' -o "$ope" == '*' -o "$ope" == '/' -o ]
? ? ? ? ? ? ? ? ? ? ? ? then
? ? ? ? ? ? ? ? ? ? ? ? res=$(($num1$ope$num2))
? ? ? ? ? ? ? ? ? ? ? ? else
#!/bin/bash
read -t 30 -p "請輸入一個數字:" num1
read -t 30 -p "請輸入一個數字:" num2
read -t 30 -p "請輸入一個運算符:" ope
#通過read命令接受要輸入的參數
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')
#定義變量test1和test2的值。把變量test值替換為空。如果能替換
#則為數字
? ? ? ? if [ -z "$test1" -a -z "$test2" ]
#判斷變量test1 2是否為空,為空證明 num1 num2是數字
? ? ? ? ? ? ? ? then
? ? ? ? ? ? ? ? if [ -n -o "$ope" == '+' -o "$ope" == '-' -o "$ope" == '*' -o "$ope" == '/' -o ]
? ? ? ? ? ? ? ? ? ? ? ? then
? ? ? ? ? ? ? ? ? ? ? ? res=$(($num1$ope$num2))
? ? ? ? ? ? ? ? else ? ?
? ? ? ? ? ? ? ? ? ? ? ? echo '輸入的運算符有誤'
? ? ? ? ? ? ? ? ? ? ? ? exit 3
? ? ? ? ? ? ? ? fi ? ? ?
? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? echo '輸入的非數字,請重新輸入'
? ? ? ? ? ? ? ? ? ? ? ? exit 2
? ? ? ? fi
else
? ? ? ? echo '未輸入,請重新輸入' ? ? ??
? ? ? ? exit 1
fi
? ? ? ? echo " $num1 $ope $num2 = $res "
2016-06-18
知道了,第一個判斷條件里有寫-n“$ope”