課程
/運(yùn)維&測(cè)試
/Linux
/shell編程之條件判斷與流程控制
計(jì)算器的腳本程序哪個(gè)美女帥哥分享下呀。。。
2019-08-23
源自:shell編程之條件判斷與流程控制 4-2
正在回答
#!/bin/bash
read -p "number1: " num1
read -p "number2: " num2
read -p "ope: " ope
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')
? ? ? ? else
? ? ? ? ? ? ? ? echo "enter number is null,try again"
? ? ? ? ? ? ? ? exit 12
fi
if [ -z "$test1" -a -z "$test2" ]
? ? ? ? ? ? ? ? echo "Please enter a valid value"
? ? ? ? ? ? ? ? exit 10
if [ "$ope"=='+' ]
? ? ? ? ? ? ? ? sum=$(( $num1 + $num2 ))
elif [ "$ope"=='-' ]
? ? ? ? ? ? ? ? sum=$(( $num1 - $num2 ))
elif [ "$ope"=='*' ]
? ? ? ? ? ? ? ? sum=$(( $num1 * $num2 ))
elif [ "$ope"=='/' ]
? ? ? ? ? ? ? ? sum=$(( $num1 / $num2 ))
else
? ? ? ? echo "Please enter a valid symbol"
? ? ? ? exit 11
echo "$num1 $ope $num2 = $sum"
#!/bin/bashread?-p?"number1:?"?num1read?-p?"number2:?"?num2read?-p?"ope:?"?opeif?[?-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')????????else????????????????echo?"enter?number?is?null,try?again"????????????????exit?12fiif?[?-z?"$test1"?-a?-z?"$test2"?]????????then????????????????echo?"Please?enter?a?valid?value"????????????????exit?10fiif?[?"$ope"=='+'?]????????then????????????????sum=$((?$num1?+?$num2?))elif?[?"$ope"=='-'?]????????then????????????????sum=$((?$num1?-?$num2?))elif?[?"$ope"=='*'?]????????then????????????????sum=$((?$num1?*?$num2?))elif?[?"$ope"=='/'?]????????then????????????????sum=$((?$num1?/?$num2?))else????????echo?"Please?enter?a?valid?symbol"????????exit?11fiecho?"$num1?$ope?$num2?=?$sum"
? ? ? ?then
? ? ? ?test1=$(echo $num1 |sed 's/[0-9]//g')
? ? ? ?test2=$(echo $num2 |sed 's/[0-9]//g')
? ? ? ?if [ -z "$test1" -a -z "$test2" ]
? ? ? ? ? ? ? ?then
? ? ? ? ? ? ? ? ? ? ? ?if [ "$ope"=='+' ]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?then
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sum=$(( $num1 + $num2 ))
? ? ? ? ? ? ? ? ? ? ? ?elif [ "$ope"=='-' ]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sum=$(( $num1 - $num2 ))
? ? ? ? ? ? ? ? ? ? ? ?elif [ "$ope"=='*' ]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sum=$(( $num1 * $num2 ))
? ? ? ? ? ? ? ? ? ? ? ?elif [ "$ope"=='/' ]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sum=$(( $num1 / $num2 ))
? ? ? ? ? ? ? ? ? ? ? ?else
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?echo "Please enter a valid symbol"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?exit 10
? ? ? ? ? ? ? ? ? ? ? ?fi
? ? ? ? ? ? ? ?else
? ? ? ? ? ? ? ? ? ? ? ?echo "Please enter a valid value"
? ? ? ? ? ? ? ? ? ? ? ?exit 11
? ? ? ?fi
? ? ? ?echo "enter number is null,try again"
? ? ? ?exit 12
哇唔,謝謝可愛的銀兒
#!/bin/bash read?-p?"please?input?a?num1:"?num1??????????#用鍵盤輸入數(shù)字 read?-p?"please?input?a?num2:"?num2 read?-p?"please?input?a?operator:"?ope #判斷是否為空 if?[?-z?"$num1"?-a?-z?"$num2"?-a?-z?"$ope"?]?#-z為空取真,中括號(hào)里兩邊需要空格 ????????then ????????echo?"is?null" ????????exit?10 fi #判斷是否為數(shù)值,將num1中數(shù)字替換為空,來進(jìn)行判斷 test1=$(echo?$num1|sed?'s/[0-9]//g') test2=$(echo?$num2|sed?'s/[0-9]//g') if?[?-n?"$test1"?-a?-n?"$test2"?]??#-n?非空取真 ????????then ????????echo?"請(qǐng)輸入數(shù)值" ????????exit?11 fi if?[?'$ope'=='+'?-o?'$ope'=='-'?-o?'$ope'=='*'?-o?'$ope'=='/'?] ????????then ????????????????sum=$(($num1$ope$num2))?#運(yùn)算要加雙括號(hào) ????????????????echo?"$num1$ope$num2=$sum" else ????????exit?12 fi
自己寫的,邏輯清晰一些 沒有嵌套
0_菇?jīng)鰧⑽易魃鮛0
舉報(bào)
Linux shell實(shí)用案例學(xué)習(xí),一定會(huì)使你Linux運(yùn)維能力再次提高
2 回答$在腳本中都在什么情況下使用
3 回答在shell計(jì)算器中如果輸入的是2b按照原來的思路就不能判斷了?
1 回答請(qǐng)問視頻中中幻燈片哪里可以下載
1 回答說好的課程在哪呢?
1 回答你好 能幫忙發(fā)一下老師講的所有視頻的先后順序嗎
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2020-11-22
#!/bin/bash
read -p "number1: " num1
read -p "number2: " num2
read -p "ope: " ope
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')
? ? ? ? else
? ? ? ? ? ? ? ? echo "enter number is null,try again"
? ? ? ? ? ? ? ? exit 12
fi
if [ -z "$test1" -a -z "$test2" ]
? ? ? ? then
? ? ? ? ? ? ? ? echo "Please enter a valid value"
? ? ? ? ? ? ? ? exit 10
fi
if [ "$ope"=='+' ]
? ? ? ? then
? ? ? ? ? ? ? ? sum=$(( $num1 + $num2 ))
elif [ "$ope"=='-' ]
? ? ? ? then
? ? ? ? ? ? ? ? sum=$(( $num1 - $num2 ))
elif [ "$ope"=='*' ]
? ? ? ? then
? ? ? ? ? ? ? ? sum=$(( $num1 * $num2 ))
elif [ "$ope"=='/' ]
? ? ? ? then
? ? ? ? ? ? ? ? sum=$(( $num1 / $num2 ))
else
? ? ? ? echo "Please enter a valid symbol"
? ? ? ? exit 11
fi
echo "$num1 $ope $num2 = $sum"
2020-11-22
2020-11-22
#!/bin/bash
read -p "number1: " num1
read -p "number2: " num2
read -p "ope: " ope
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')
? ? ? ?if [ -z "$test1" -a -z "$test2" ]
? ? ? ? ? ? ? ?then
? ? ? ? ? ? ? ? ? ? ? ?if [ "$ope"=='+' ]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?then
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sum=$(( $num1 + $num2 ))
? ? ? ? ? ? ? ? ? ? ? ?elif [ "$ope"=='-' ]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?then
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sum=$(( $num1 - $num2 ))
? ? ? ? ? ? ? ? ? ? ? ?elif [ "$ope"=='*' ]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?then
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sum=$(( $num1 * $num2 ))
? ? ? ? ? ? ? ? ? ? ? ?elif [ "$ope"=='/' ]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?then
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sum=$(( $num1 / $num2 ))
? ? ? ? ? ? ? ? ? ? ? ?else
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?echo "Please enter a valid symbol"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?exit 10
? ? ? ? ? ? ? ? ? ? ? ?fi
? ? ? ? ? ? ? ?else
? ? ? ? ? ? ? ? ? ? ? ?echo "Please enter a valid value"
? ? ? ? ? ? ? ? ? ? ? ?exit 11
? ? ? ?fi
else
? ? ? ?echo "enter number is null,try again"
? ? ? ?exit 12
fi
echo "$num1 $ope $num2 = $sum"
2019-08-29
哇唔,謝謝可愛的銀兒
2019-08-29
自己寫的,邏輯清晰一些 沒有嵌套