Shell scripting Learn (2) compare two digit sizes

Source: Internet
Author: User

Note: The comparison string in the shell can only be used = =, <, >,! =,-Z,-N. When comparing strings, be sure to add X (or a, B, etc.) to the end of the string, because if [$1x = = "AB" x] If there is no X, and "" ", this statement will be translated into if [= =" AB "], the left equivalent of nothing, will report a syntax error. or using [[]], you don't need x. When using < or >, if [] is used, the escape character "\" is required, such as \>.

Compare numbers using-eq,-ne,-gt,-ge,-lt,-le, or = =, <, >,! =. Where-eq means that Equal,-ne is unequal,-gt is greater than,-ge is greater than or equal to,-lt is less than,-le is less than or equal to.

[[Email protected]SH]$CatNumber_compare.SH #!/bin/bash# script Name: Number_compare.SH#用途: Compare two digit sizesEcho "Please input first number :"Read xEcho  "You first number x= $x"Read-P"Please input second number:"yEcho  "You second number y= $y"if[$x-eq $y]; Then    Echo "Equal"elif[$x-gt $y]; Then    Echo "x greater than Y"Else    Echo "x less than y"fi

To run the test:

[[Email protected]SH]$./number_compare.SHPlease input first number : theYou first number x= thePlease input second number: atYou second number y= atx greater than Y
[[Email protected]SH]$./number_compare.SHPlease input first number : AYou first number x= APlease input second number: AYou second number y= Aequal
[[Email protected]SH]$./number_compare.SHPlease input first number : atYou first number x= atPlease input second number: AboutYou second number y= Aboutx Lessthan Y

Shell scripting Learn (2) compare two digit sizes

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.