Conditional test statements in the shell

Source: Internet
Author: User


The shell has conditional test statements, which are usually done using the test command or the [] command, which is the basis of the conditional Judgment statement If~then statement, especially the [] command. Let's explain some of the conditional test statements below.


1. Test command Implementation condition testing

The test command is useful for detecting the existence of certain files in the system, or for related properties.

The basic syntax is as follows:


The test command also tests the string:



The test command also tests the relationships between integers:



2. [] command to implement condition testing

You should pay special attention when using the brackets [] command to implement the conditional test function. Because the brackets are useful in many places, such as a wildcard wildcards regular expression. When we use [] to represent condition testing, we must follow the following principles:

1) each component within the brackets [] needs to be separated by a space bar, for example:[-"$HOME"-==-"$MAIL"-]

2) in brackets, it is best to enclose the variable in double quotation marks;

3) Constants in brackets, most of which are enclosed in single or double quotation brackets.

[] command to implement conditional tests, typically used in conditional judgment statements for If~then.


Reference:

"Brother Bird's Linux private dish"


Conditional test statements in the shell

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.