Shell Script ——— Compare integer size classic case

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.