Method 1: Pass the parameter
#!/bin/bash
##############################################################
# File Name:compare1.sh
# version:v7.4
# Author:feng Yu
# organization:http://blog.51cto.com/13520761
# Created Time:2018-03-26 17:22:20
# Description:
##############################################################
[$#-ne 2] && echo "Please enter 2 parameters" && Exit 2 ---The number of parameters is not equal to 2 o'clock, please enter 2 parameters and exit
Expr + 1 &>/dev/null
[$?-eq 2] && echo "Please enter integer" && exit 2 ---Determine if the first argument is an integer, not an integer, enter an integer, and exit
Expr + 1 &>/dev/null
[$?-eq 2] && echo "Please enter integer" && exit 2 ---Determine if the second argument is an integer, not an integer, enter an integer, and exit
[$1-eq] && echo "$ = $"
[$1-LT] && echo "$ < $"
[$1-GT] && echo "$ > $"
Method 2: Pass the parameter (if expression)
#!/bin/bash
##############################################################
# File Name:compare.sh
# version:v7.4
# Author:feng Yu
# organization:http://blog.51cto.com/13520761
# Created Time:2018-03-26 16:20:43
# Description:
##############################################################
if! [$#-eq 2];then
echo "Please enter 2 parameters"
Exit
Fi
Expr + 1 &>/dev/null
If [$?-eq 2];then
echo "Please enter an integer"
Exit
Fi
Expr + 1 &>/dev/null
If [$?-eq 2];then
echo "Please enter an integer"
Exit
Fi
If [$1-eq $];then
echo "$ = $"
elif [$1-lt $];then
echo "$ < $"
elif [$1-gt $];then
echo "$ > $"
Fi
Method 3:read Read in
#!/bin/bash
##############################################################
# File Name:compare2.sh
# version:v7.4
# Author:feng Yu
# organization:http://blog.51cto.com/13520761
# Created Time:2018-03-26 18:20:14
# Description:
##############################################################
Read-p "Please enter an integer:" Num1
Expr $num 1 + 1 &>/dev/null
[$?-eq 2] && echo "Please enter an integer" && exit 2
[-Z] $num 1 "] && echo" Please enter an integer "&& exit 2
Read-p "Please enter an integer:" num2
Expr $num 2 + 1 &>/dev/null
[$?-eq 2] && echo "Please enter an integer" && exit 2
[-Z] $num 2 "] && echo" Please enter an integer "&& exit 2
[$num 1-eq $num 2] && echo "$num 1 = $num 2"
[$num 1-gt $num 2] && echo "$num 1 > $num 2"
[$num 1-lt $num 2] && echo "$num 1 < $num 2"
Method 4:read Read-in (if expression)
#!/bin/bash
##############################################################
# File Name:compare2.sh
# version:v7.4
# Author:feng Yu
# organization:http://blog.51cto.com/13520761
# Created Time:2018-03-26 18:20:14
# Description:
##############################################################
Read-p "Please enter an integer:" Num1
Expr $num 1 + 1 &>/dev/null
If [$?-eq 2];then
echo "Please enter an integer"
Exit 2
Fi
If [-z] $num 1 "];then
echo "Please enter an integer"
Exit 2
Fi
Read-p "Please enter an integer:" num2
Expr $num 2 + 1 &>/dev/null
If [$?-eq 2];then
echo "Please enter an integer"
Exit 2
Fi
If [-Z] $num 2 "];then
echo "Please enter an integer"
Exit 2
Fi
If [$num 1-eq $num 2];then
echo "$num 1 = $num 2"
elif [$num 1-gt $num 2];then
echo "$num 1 > $num 2"
elif [$num 1-lt $num 2];then
echo "$num 1 < $num 2"
Fi
Shell Script ——— Compare integer size classic case