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