Conditional judgment and comparison of shell scripts

Source: Internet
Author: User

common syntax for conditional testing:

Syntax format Description
[< test expression >] [] has at least one space between the boundary and the content
[[ < Test expression > ]] [[] There is at least one space between the boundary and the content

Note that the:&&, | |, >, < operators can be applied to [[]], but not to [], and in [] generally use-A,-O,-GT (for integers) instead of the above operators

Commonly used file-judging operators



-D files, all of which are spelled as directory The file exists and is true for the directory, that is, the test expression is set
-f file, F's full spell file File exists and is normal file True, that is, the test expression is set
-e file, the full spell for exist The existence of the file is true, that is, the test expression is established. -E does not distinguish between a directory or a file
-r/w/x file, r/w/x's full spell is read/write/executable File exists and readable/writable/executable, i.e. test expression established
-S file, S is fully spelled as size True if the file exists and the file size is not 0
-l file, L's all spelled as Link The file exists and is true for the linked file
F1-nt F2,nt's full spell is newer than File F1 newer than file F2 true
F1-ot F2,ot's full spell is older than File F1 is true compared to file F2 old

Commonly used string test expressions


-N "String" True if the length of the string is not 0
-Z "string" True if the length of the string is not 0

operator comparison

Comparison symbols used in test and []
Comparison symbols used in (()) and [[]] Description
-eq = = or = Equals
-ne != Not equal to
-gt > Greater than
-ge >= Greater than or equal
-lt < Less than
-le <= Less than or equal
-A
&& And
-O || Or
! ! Non -


Conditional judgment and comparison of shell scripts

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.