if 怎样改成非嵌套
IF{}
elIF{}
IF{}
elIF{}
2015-06-15
#!/bin/bash echo "Hi, this is a calculater" read -t 30 -p "Please input num1:" num1 read -t 30 -p "Please input num2:" num2 read -t 30 -p "Please input the operator(+-*/):" ope if [ -z "$num1" -o -z "$num2" -o -z "$ope" ] then echo "Sorry, Missed input" exit 12 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 "Sorry, Invalid Value" exit 11 fi if [ "$ope" == "+" ] then res=$(($num1+$num2)) echo "+ operated" elif [ "$ope" == "-" ] then res=$(($num1-$num2)) echo "- operated" elif [ "$ope" == "*" ] then res=$(($num1*$num2)) echo "* operated" elif [ "$ope" == "/" ] then if [ "$num2" -eq "0" ] then echo "Sorry, Can not do this, Bye" exit 10 else res=$(($num1/$num2)) echo "/ operated" fi else echo "Sorry, Invalid Operator" exit 9 fi echo "$num1 $ope $num2 : $res" echo "Thanks"
#!/bin/bash 2 3 read -p "Please input your firstnum:" num1 4 read -p "Please input your secondnu:" num2 5 read -p "please input your operation:" ope 6 test1=$(echo $num1 | grep "[^0-9]") 7 test2=$(echo $num2 | grep "[^0-9]") 8 if [ -z "$num1" -o -z "$num2" -o -z "$ope" ] 9 then 10 echo "Input is NULL" 11 exit 10 12 fi 13 if [ -n "$test1" -o -n "$test2" ] 14 then 15 echo "Input is not number" 16 exit 11 17 fi 18 19 if [ "$ope" == '+' ] 20 then 21 result=$(($num1 + $num2)) 22 elif [ "$ope" == '-' ] 23 then 24 result=$(($num1 - $num2)) 25 elif [ "$ope" == '*' ] 26 then 27 result=$(($num1 * $num2)) 28 elif [ "$ope" == '/' ] 29 then 30 result=$(($num1 / $num2)) 31 else 32 echo "Ope is error" 33 exit 12 34 fi 35 echo $result
#我这个方法比老师的简单,判断空不用那么麻烦
1 #!/bin/bash
2
3 read -p "Please input your firstnum:" num1
4 read -p "Please input your secondnu:" num2
5 read -p "please input your operation:" ope
6 test1=$(echo $num1 | grep "[^0-9]")
7 test2=$(echo $num2 | grep "[^0-9]")
8 if [ -z "$num1" -o -z "$num2" -o -z "$ope" ]
9 then
10 echo "Input is NULL"
11 exit 10
12 fi
13 if [ -n "$test1" -o -n "$test2" ]
14 then
15 echo "Input is not number"
16 exit 11
17 fi
18
19 if [ "$ope" == '+' ]
20 then
21 result=$(($num1 + $num2))
22 elif [ "$ope" == '-' ]
23 then
24 result=$(($num1 - $num2))
25 elif [ "$ope" == '*' ]
26 then
27 result=$(($num1 * $num2))
28 elif [ "$ope" == '/' ]
29 then
30 result=$(($num1 / $num2))
31 else
32 echo "Ope is error"
举报