Compare if in shell

Source: Internet
Author: User
In shell, if is used to compare whether two strings are equal: www.2cto. comif [& quot; $ test & quot; x & quot; test & quot; x]; then has the following key points: 1. use a single equal sign 2 to notice that there is a space on both sides of the equal sign: this is the unixshell... in shell, the key to comparing two strings is: www.2cto.com if ["$ test" x = "test" x]; then: 1. use a single equal sign 2 to notice that there is a space on both sides of the equal sign: this is a unix shell requirement 3. Note that the last x of "$ test" x is specially arranged, because when $ test is empty, the above expression becomes
X = testx, which is obviously not equal. Without this x, the expression returns the following error: [: =: unary operator expected binary comparison operator, comparison variable or comparison number. note the difference between numbers and strings. integer comparison-eq is equal to, for example, if ["$ a"-eq "$ B"]-ne is not equal to, for example: if ["$ a"-ne "$ B"]-gt is greater than, for example, if ["$ a"-gt "$ B"]-ge is greater than or equal to, for example: if ["$ a"-ge "$ B"]-lt is less than, for example, if ["$ a"-lt "$ B"]-le is less than or equal to, for example: if ["$ a"-le "$ B"] is greater than (double parentheses are required), such as: ("$ a"> "$ B")> = greater than or equal to (double parentheses are required), such as: ("$ a"> = "$ B") for small data comparison, you can use an AWK string to compare www.2cto.com = equal to, for example: if ["$ a" = "$ B"] = equal to, for example, if ["$ A" = "$ B"], which is equivalent to =. note: The = function has different behaviors in [] and [], as shown below: 1 [[$ a = z *] # if $ a starts with "z" (pattern matching) true 2 [[$ a = "z *"] # if $ a is equal to z * (character matching ), the result is true 3 4 [$ a = z *] # File globbing and word splitting will occur 5 ["$ a" = "z *"] # if $ equal to z * (character matching ), the result is true. File globbing is a stenographer about a File, for example "*. c "is, like ~ Yes. but file globbing is not a strict regular expression. Although the structure is similar in most cases .! = Not equal to, such as: if ["$ "! = "$ B"] this operator uses pattern matching in the [[] structure. greater than, in the ASCII alphabetic order. for example, if [["$ a"> "$ B"] if ["$ a" \> "$ B"] Note: in the [] structure, ">" must be escaped. for more information, see Example 26-11. -the z string is "null ". the length is 0. -n string is not "null" Note: to use-n to test in the [] structure, you must use "" to cause the variable. use a string that is not! -Z or a string that is not referenced by "", put it in the [] structure. Although it can work normally, it is not safe. it is a good habit to use "" to test strings. if condition-based if [condition-based judgment 1] & (|) [condition-based judgment 2]; thenelif [condition-based judgment 3] & (|) [condition-based judgment 4]; what is the third stage of thenelse execution? Hot? /Div> fi for example: root @ Bizbox :~ # A = 0root @ Bizbox :~ # B = 0root @ Bizbox :~ # C = 5 root @ Bizbox :~ # If [$ a = 0-a $ B = 0] & [$ c! = 0]; then> echo success> the expression Primary used by fisuccessif indicates that the [-a file] is true if the file exists. [-File B] if the file exists and is a block-the special file is true. The [-c file] is true if the file exists and is a word-special file. The [-d file] is true if the file exists and is a directory. The [-e file] is true if the file exists. [-F file] is true if the file exists and is a common file. The [-g file] is true if the file exists and its SGID bit has been set. The [-h file] is true if the file exists and is a symbolic connection. [-K file] is true if the file exists and its sticky position has been set. The [-p file] is true if the file exists and is a named pipe (F if O ). [-R file] is true if the file exists and is readable. The [-s file] is true if the file exists and is larger than zero bytes. [-T FD] true if the file descriptor has been opened and points to a terminal. [-U file] is true if the file exists and its SUID (set user ID) bit has been set. [-W file] true if the file is true if the file exists and is writable. The [-x file] is true if the file exists and is executable. The [-O file] is true if the file exists and is a valid user ID. The [-G file] is true if the file exists and belongs to a valid group ID. [-L file] is true if the file exists and is a symbolic connection. [-N File] true if the file exists and has been mod if ied since it was last read. The [-S file] is true if the file exists and is a socket. [File 1-nt File 2] true if file 1 has been changed more recently than file 2, or if file 1 exists and file 2 does not. [File 1-ot File 2] true if file 1 is older than file 2, or file 2 exists and file 1 does not exist. [File 1-ef File 2] true if file 1 and file 2 refer to the same device and inodenumbers. [-O option name] true if shell option "option name" is enabled. [-Z STRING] true if the length of "STRING" is zero. The length of [-n STRING] or [STRING] is a non-zero value. [STRING1 = STRING2] if the two strings are equal to true. "=" May be used instead of "=" forstrict POSIX compliance. [STRING1! = STRING2] true if the two strings are not equal. [STRING1 <STRING2] is true if "STRING1" sorts before "STRING2" lexicographically in thecurrent locale. [STRING1> STRING2] is true if "STRING1" sorts after "STRING2" lexicographically in thecurrent locale. [ARG1 OP ARG2] "OP" is-eq,-ne,-lt,-le,-gt or-ge. These arithmetic binary operatorsreturn true if "ARG1" is equal to, not equal to, less than, less than or equal to, greaterthan, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" areintegers. The expression can be combined by the following operators: listed in decreasing order of precedence: Operator effect www.2cto.com [! EXPR] true if EXPR is false. [(EXPR)] returns the value of EXPR. This can be used to ignore normal operator priorities. [Expression 1-a expression 2] true if expression 1 and expression 2 are both true. [Expression 1-o expression 2] If expression 1 or expression 2 is true, it is true.
 
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.