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:

评论

暂无评论