"Learn Linux with older boys Koriyuki shell programming Practice"-fifth Chapter shell script condition test

Source: Internet
Author: User
Tags logical operators

The knowledge points in this article are related to conditional testing of shell scripts.

Often in shell scripts we need to do a variety of conditional judgments, such as testing whether a file exists, whether it is a file or directory, whether

has execute permissions and so on, so in a shell script, conditional judgment is still critical. Next we get to the point: the conditional test of the shell script.

1. In bash programming, the usual syntax form for conditional testing is as follows:

Tips:

    • The test command in Syntax 1 is equivalent to [] in Syntax 2. The [[]] double brackets in syntax 3 are the extended test commands.

      Syntax 4 (()) is often used for calculations.

    • &&, | |, >, < The equality operator can be applied to double brackets {[[]], but not to single brackets [], in [] generally with-a,-O,

      -gt (for integers), -lt (for integers) instead of the above operator.

    • for integer relational operations, it can also be an arithmetic character (()) using the shell.


The following is a detailed description of the common condition test syntax:

(1) Simple syntax and examples of test condition tests:

Syntax format for test conditional tests: Test condition expression

Example 1: Test file exists:

Test-f file && echo true | | echo False

==> Test if file exists, output true if present, otherwise output false

Example 2: Test whether the length of the string is 0:

[[email protected] ~]# test-z "Xiaoyu" && echo true | | echo False
False

==> if the length of the test string is 0, the expression is true because the string tested is Xiaoyu and not 0, the result of the expression is false.

(2) [] brackets conditional test syntax and examples

Syntax format: [conditional test expression] ==> a space at both ends of the bracket

Example 1: Test for the existence of file files:

[-F file] && echo 1 | | Echo 2 ==> test file exists, there is return 1, there is no return 0.

(3)[[]] double-bracket conditional test syntax and examples

Syntax format: [[conditional test expression]] ==> double brackets must have spaces in both ends

Example 1:[[-f file]] && echo 1 | | echo 0 ==> The test file exists, returns 1 if present, does not exist return 0.

Tips:

in [ []] , you can use wildcard characters and so on to pattern matching, and &&, | |, >, < and other operators can be applied in [ []] , but not in [],

In [] generally use the-a,-O,-gt (for integers),-lt (for integers), and other operators instead of the symbols mentioned above for [[]] .

Tips:

although the [[]] application scenario is not many, but in the [[]] wildcard match scenario, the other test expression cannot be substituted, if the wildcard match is required

Or regular match is used [[]].

2. File Test expression:

Commonly used file test operators:

Tips:

1. test file read, write, executable and other properties, not only according to the file attributes rwx identification, but also to see whether the user currently executing the test

It is true that the file can be manipulated according to the appropriate permissions.

2. When testing a variable with the [] bracket, double quotation marks are required if the object being tested is a variable.

3. Special condition test expression case

The following notation applies to all conditional test expressions and is a common alternative to if statements in work.

Example 1: When condition 1 is established, execute command 1, command 2, and command 3 simultaneously:

The above judgment is equivalent to the effect of the following if statement:

4. String Test expression

String test operators:

5. Integer Two-dollar comparison operator

The integer two-dollar comparison operator uses the reference:

Tips:

(1) "=" and "! =" can also be used for comparison in [], but when using symbols containing ">" and "<" in [], you need to escape, sometimes

not escaped, Although the syntax does not error, the result may be incorrect.

(2) You can also use symbols that contain "-gt" and "LT" in [[]], but are not recommended for this use.

(3) The comparison symbol must also have a space at both ends.

(4) Conditional test expressions, note that ordinary characters compare common string comparison methods, that is, double quotation marks comparison, rather than using integer comparison of the syntax,

Integers are more prone to error unless determined to be integers.

It is recommended to use the [] single-bracket method for integer comparisons.

Note: Although the method in the tips can also be used, it is recommended to use the reference as described above.

6. Logical operators

Tips:

(1) The "-a" and "-o" logical operators need to be used in [].

(2) "&&" and "| |" The logical operation symbol can be used in [[]] and (()), or it can be externally connected to multiple [].

(3) Note that there must be a space at both ends of [] and [[]], but not required for (()).

Summarize:

A summary of the differences between test expressions, [], [[]], (()):




Above for I read "with the old boy learn Linux Yun Koriyuki Shell programming Combat" This book when the note, if there are any copyright issues, please contact the message.

This article is from the "shayatou_1990" blog, make sure to keep this source http://shayatou1990.blog.51cto.com/12806916/1919584

"Learn Linux with older boys Koriyuki shell programming Practice"-fifth Chapter shell script condition test

Related Article
Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.