Shell condition judgment

Source: Internet
Author: User

Traditional if statements -- use conditional expressions as if conditions
If [conditional expression]
Then
Command
Command
Command
Else
Command
Command
Fi

Conditional expressions

File expression
If [-F file] If the file exists
If [-d...] If the directory exists
If [-s file] If the file exists and is not empty
If [-r file] If the file exists and is readable
If [-W file] If the file exists and can be written
If [-X file] If the file exists and can be executed

Integer variable expression
If [int1-EQ int2] If int1 is equal to int2
If [int1-ne int2] is not equal
If [int1-ge int2] If> =
If [int1-GT int2] If>
If [int1-Le int2] If <=
If [int1-lt int2] If <


String variable expression
If [$ A = $ B] If string1 is equal to string2
A string can be equal by a value.
If [$ string1! = $ String2] If string1 is not equal to string2
If [-N $ string] If the string is not empty (not 0), 0 (true) is returned)
If [-Z $ string] is null
If [$ Sting] If the string is not null, 0 is returned (similar to-N)

Non-logical! Inverse of conditional expressions
If [! Expression]
If [! -D $ num] If the directory does not exist $ num


Parallel Logic with-a conditional expression
If [expression 1-A expression 2]


Or of a logical or-O condition expression
If [expression 1-O expression 2]


Logical expression

Expression and the previous =! =-D-F-X-ne-eq-lt
Logical symbols are normally connected to other expressions without any parentheses ().
If [-z "$ jhhome"-a-d $ home/$ num]

Note that logic and-A and logic or-O are easily mixed with the operator numbers of other strings or files.


The most common assignment method is to evaluate the variables = both sides before the assignment.
Whether the test variable on the left is empty, and whether the test directory (value) On the right exists (value is valid)

If test $ num-EQ 0 is equivalent to if [$ num-EQ 0]

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.