Shell integer comparison

Source: Internet
Author: User

To test the comparison of integer types in shell programming, the following test code is specially written:

#! /Bin/sh </P> <p> # adding single quotation marks to variable values in shell. The difference between double quotation marks and double quotation marks is irrelevant to the type, it is mainly because the single quotation mark is not <br/> # Replace the relevant amount. For example, $ is interpreted as a variable reference and replaced by a value, double quotation marks are replaced. </P> <p> A = "$1" <br/> B = "$2" </P> <p> echo "indicates the original input value.: A = $ A, B = $ B "</P> <p> # The following comparison can only be used to compare integers (not decimals ), otherwise, the error <br/> # integer expression expected </P> <p> echo "shell style:" <br/> If [$ A-EQ $ B]; then <br/> echo "EQ" <br/> fi </P> <p> If [$ A-ge $ B]; then <br/> echo "Ge" <br/> fi </P> <p> If [$ A-Le $ B]; then <br/> echo "Le" <br/> FI </P> <p> If [$ A-ne $ B]; then <br/> echo "ne" <br/> fi </P> <p> If [$ A-GT $ B]; then <br/> echo "GT" <br/> fi </P> <p> If [$ A-lt $ B]; then <br/> echo "LT" <br/> fi </P> <p> echo "Comparison of c styles" <br/> # The following c styles are also OK, but it mainly requires double brackets <br/> If ($ A = $ B )); then <br/> echo "=" <br/> fi </P> <p> If ($ A >=$ B )); then <br/> echo "> =" <br/> fi </P> <p> If ($ A <= $ B )); then <br/> echo "<=" <br/> fi </P> <p> If ($! = $ B); then <br/> echo "! = "<Br/> fi </P> <p> If ($ A> $ B )); then <br/> echo ">" <br/> fi </P> <p> If ($ A <$ B )); then <br/> echo "<" <br/> fi </P> <p>

 

Perform the following operations on the console:

1. Equal Integers

./Test_compareop.sh 123 123

Output:

Input original values: A = 123, B = 123
Shell style:
EQ
Ge
Le
Comparison of C-style
=
> =
<=

 

2. integers greater

./Test_compareop.sh 123 12

Output:

Input original values: A = 123, B = 12
Shell style:
Ge
Ne
GT
Comparison of C-style
> =
! =
>

 

3. Integer smaller

./Test_compareop.sh 123 124

Output:

Input original values: A = 123, B = 124
Shell style:
Le
Ne
Lt
Comparison of C-style
<=
! =
<

 

4. character format

./Test_compareop.sh 123 ABC

Output:

Input original values: A = 123, B = ABC
Shell style:
./Test_compareop.sh: Line 16: [: ABC: integer expression expected
./Test_compareop.sh: line 20: [: ABC: integer expression expected
./Test_compareop.sh: Line 24: [: ABC: integer expression expected
./Test_compareop.sh: Line 29: [: ABC: integer expression expected
./Test_compareop.sh: Line 33: [: ABC: integer expression expected
./Test_compareop.sh: line 37: [: ABC: integer expression expected
Comparison of C-style
> =
! =
>

 

It can be seen that the C-style format supports string comparison.

 

5. Floating Point format

./Test_compareop.sh 123.34 233.5

Output:

Input original values: A = 123.34, B = 233.5
Shell style:
./Test_compareop.sh: Line 16: [: 123.34: integer expression expected
./Test_compareop.sh: line 20: [: 123.34: integer expression expected
./Test_compareop.sh: Line 24: [: 123.34: integer expression expected
./Test_compareop.sh: Line 29: [: 123.34: integer expression expected
./Test_compareop.sh: Line 33: [: 123.34: integer expression expected
./Test_compareop.sh: Line 37:[: 123.34: integer expression expected
Comparison of C-style
./Test_compareop.sh: Line 43: (: 123.34 = 233.5: syntax error in expression (error token is ". 34 = 233.5 ")
./Test_compareop.sh: line 47: (: 123.34> = 233.5: syntax error in expression (error token is ". 34> = 233.5 ")
./Test_compareop.sh: line 51: (: 123.34 <= 233.5: syntax error in expression (error token is ". 34 <= 233.5 ")
./Test_compareop.sh: line 55: (: 123.34! = 233.5: syntax error in expression (error token is ". 34! = 233.5 ")
./Test_compareop.sh: Line 59: (: 123.34> 233.5: syntax error in expression (error token is ". 34> 233.5 ")
./Test_compareop.sh: Line 63: (: 123.34 <233.5: syntax error in expression (error token is ". 34 <233.5 ")

As you can see, both of them have errors.

 

Summary

1. [$ A-op $ B] only supports integer comparison, where-op = [-EQ |-lt |-GT |-ne |-ge |-le], a space must be reserved on both sides of the brackets.

2. C-style comparison supports comparison of integers and strings, but does not support comparison of floating-point numbers.

3. The values of single quotes, double quotation marks, and variables without quotation marks in shell do not affect the type, but affect the corresponding escape. For example, $ in double quotation marks will be considered as a variable, shell automatically replaces the value with the variable value, but the single quotation mark does not

4. Note that there must be spaces on both sides of the comparison symbol, for example, [$ A = $ B], but not [$ A = $ B].

For example, if ["aabdc" = "ABC"] does not contain spaces and does not conform to the comparison syntax. Shell considers it a simple string, in Shell conditions, the character is true.

Must be changed to ["aabdc" = "ABC"]

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.