Shell编程之if简单判断两个数字大小


#脚本编辑


 

#!/bin/bash

#定义变量
num1=$1
num2=$2
 
#判断是否输入两个参数,若是,将两个参数传递给下一个指令动作,若非两个参数,则打印输出内容输出并且退出exit脚本不执行下一个指令
if [ $# -ne 2 ] ;then
   echo 'please input num1 & num2:'
exit
fi
 
#以上判断后,输入的两个参数将传递到如下指令判断
if [ $num1 -gt $num2 ] ; then
   echo $num1 great than $num2
 
else if [ $num1 -lt $num2 ] ; then
   echo $num1 less than $num2
 
else
   echo $num1 equal $num2
 
fi  
fi
 

 
#验证是否正确
 
[root@lin scripts]# sh compare2.sh 66
please input num1 & num2:
[root@lin scripts]# sh compare2.sh 10 10
10 equal 10
[root@lin scripts]# sh compare2.sh 10 1
10 great than 1
[root@lin scripts]# sh compare2.sh 10 50
10 less than 50
[root@lin scripts]# sh compare2.sh 6 7 8 9 10
please input num1 & num2:
 
 
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM