課程
/運維&測試
/Linux
/shell編程之條件判斷與流程控制
IF{}
elIF{}
2015-06-15
源自:shell編程之條件判斷與流程控制 4-2
正在回答
#!/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"
慕妹2006075 提問者
?#!/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"
這個實在是太簡單了。
舉報
Linux shell實用案例學習,一定會使你Linux運維能力再次提高
2 回答第三個嵌套if簡化成這樣,沒啥漏洞吧,求檢測
1 回答elif后面可以不接fi,遇到嵌套條件怎么辦
1 回答老師交待的作業,不用if嵌套寫了個加減乘除,大神們有BUG嗎?
3 回答已經看完 ... 非常感謝
1 回答shell編輯計算器非數字BUG
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2015-10-01
2016-01-13
2016-01-13
? #我這個方法比老師的簡單,判斷空不用那么麻煩
?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"
2015-09-12
這個實在是太簡單了。