Linux shell logical operators, logical expressions

Source: Internet
Author: User
Tags logical operators

The logical operators of the shell involve the following types, so as long as the appropriate choice, can solve our many complex judgments, to achieve a multiplier effect.

First, the logical operator

Logical Volume Label Express meaning
1. About the file and directory of the detection of Logical volume label!
-F Common! Detect if "Archive" exists eg:if [-f filename]
-D Common! Detects if the directory exists
-B Detects if it is a "block file"
-C Detects if it is a "character file"
-S Detects if it is a "socket tag file"
-L Detects if it is a "symbolic Link's profile"
-E Detect if "something" exists!
2. About the logical volume label of the program!
-G Detect whether the program owned by the GID
-O Detection is owned by the program executed by the UID
-P Detects if a name pipe or FIFO is being transmitted between programs (to be honest, this is not a good idea!). )
3. About the property detection of the file!
-R Detect if a property is readable
-W Detects if a property can be written
-X Detect whether the property is executable
-S Detects if it is a "non-blank file"
-U Detect if a property with "SUID" is
-G Detect if a property with "SGID" is
-K Detect if a property with "sticky bit" is
4. judgments and comparisons between two files ; for example [Test file1-nt file2]
-nt The first file is newer than the second one.
-ot The first file is older than the second one.
-ef The first file is the same file as the second file (link and other files)
5. Logical AND (and) "or (OR)"
&& The meaning of the logical and
|| The meaning of a logical OR

Operation symbols Representative meaning
= Equal to: Integer or string comparison if in [], only the string
!= Not equal to: integer or string comparison if in [], only the string
< Less than applied: integer comparison in [], cannot use the representation string
> Greater than applied: integer comparison in [], cannot use the representation string
-eq equals applies To: integer comparison
-ne Not equal to: integer comparison
-lt Less than applied to: integer comparison
-gt Greater than applied to: integer comparison
-le Less than or equal to: integer comparison
-ge Greater than or equal to: integer comparison
-A Both sides established (and) logical Expressions –a logical expressions
-O Single-sided (or) logical expression –o logical expression
-Z Empty string
-N Non-empty string

Ii. Logical Expressions

    • Test command

How to use:test EXPRESSION

Such as:

[[email protected] ~]# Test 1 = 1 && echo ' OK '
Ok

[[email protected] ~]# test-d/etc/&& echo ' OK '
Ok

[[email protected] ~]# Test 1-eq 1 && echo ' OK '
Ok

[[Email protected] ~]# if test 1 = 1; Then echo ' OK '; Fi
Ok

Note: All characters are separated from the logical operators directly with "spaces" and cannot be joined together.

    • Thin expressions
    • [] Expression

[[Email protected] ~]# [1-eq 1] && echo ' OK '
Ok

[[Email protected] ~]# [2 < 1] && echo ' OK '
-bash:2: No such file or directory

[Roo[email protected] ~]# [2 \< 1] && echo ' OK '

[[Email protected] ~]# [2-GT 1-a 3-lt 4] && echo ' OK '

OK

[[Email protected] ~]# [2-GT 1 && 3-lt 4] && echo ' OK '
-bash: [: Missing '] '

Note: In the [] expression, the common >,< need to add an escape character, which represents the string size comparison, as compared to the Acill code position. Do not directly support the <> operator, there are logical operators | | && it needs to be represented by-a[and]–o[or]

  • [[]] expressions
[[Email protected] ~]# [1-eq 1] && echo ' OK '
Ok

[[Email protected] ~]$ [[2 < 3]] && echo ' OK '
Ok

[[Email protected] ~]$ [[2 < 3 && 4 > 5]] && echo ' OK '
OK

Note: the [[]] operator is only an extension of the [] operator. The ability to support the <,> symbol operation does not require an escape character, it is also a string comparison size. Logical operators are supported inside: | | &&

Linux shell logical operators, logical expressions

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.