溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用SHELL完成兩個數(shù)字的大小比較

發(fā)布時間:2020-08-05 16:46:19 來源:網(wǎng)絡(luò) 閱讀:1417 作者:弓長善慈 欄目:開發(fā)技術(shù)

簡要說明:

提示用戶輸入兩個數(shù)字;

判斷輸入的內(nèi)容是否都為數(shù)字;

數(shù)字做計算并反饋結(jié)果;

計算完畢后詢問客戶是否繼續(xù)使用;

給用戶提供隨時退出的方法。

[root@lb02 scripts]# cat jisuan2.sh 

#!/bin/bash


###thank_oldboy

###2016/3/6

###i wish you all the best.

. /etc/init.d/functions


### read number.

function read_number(){

 read -p "please enter the number1:" num1

 read -p "please enter the number2:" num2

 panduan_number

}


### judge $number1 $number2 both are number.

function panduan_number() {

 expr $num1 + $num2 &>/dev/null

 [ $? -ne 0 ] && {

  [[ $num1 == exit || $num2 == exit ]] && {

   echo "you had choice leave,bye."

   exit 0

  } || {

  echo "USAGE:you must enter two numbers, or enter exit to leave."

  read_number

  }

 } || judge_number

}


### judge continue or exit.

function continue_or_exit() {

 read -p "please enter go to continue or enter out to leave: " str

 case "$str" in 

  go)

     read_number

     

     ;;

  out)

     echo "hope to see you again,bye."

     exit 0


     ;;

  *)

     echo "USAGE:enter go or out:"

     continue_or_exit

     ;;

 esac

}


### expr $number1 $number2.

function judge_number() {

[ $num1 -eq $num2 ] && { 

 echo "$num1 equal $num2."

 continue_or_exit

}

[ $num1 -gt $num2 ] && {

 echo "$num1 greater than $num2."

 continue_or_exit

}

[ $num1 -lt $num2 ] && {

 echo "$num1 lower than $num2."

 continue_or_exit

}

}



### the gate of the script. 

function main() {

 read_number

}

main


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI