码迷,mamicode.com
首页 > 系统相关 > 详细

使用SHELL完成两个数字的大小比较

时间:2016-03-06 23:56:49      阅读:713      评论:0      收藏:0      [点我收藏+]

标签:shell 两个数比较大小

简要说明:

提示用户输入两个数字;

判断输入的内容是否都为数字;

数字做计算并反馈结果;

计算完毕后询问客户是否继续使用;

给用户提供随时退出的方法。

[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


使用SHELL完成两个数字的大小比较

标签:shell 两个数比较大小

原文地址:http://10546390.blog.51cto.com/10536390/1748133

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!