Linux shell logical operators, logical expressions detailed introduction _linux shell

Source: Internet
Author: User
Tags logical operators

One, logical operators

Logical Volume Label Express meaning
1. About the files and directories of the detection of logical labels!
-F Common! Detect the existence of "archives" eg:if [-f filename]
-D Common! Detect if "directory" exists
-B Detect whether it is a "block file"
-C Detect whether it is a "character file"
-S Detect whether it is a "socket tag file"
-L Detect if it is a "symbolic link file"
-E Detect if "something" exists!
2. About the program's Logical volume label!
-G Detects whether the program performed by the GID has
-O Detection is owned by the program executed by the UID
-P Name pipe or FIFO that detects whether to send information between programs (frankly, this is not very understanding!) )
3. About the properties of the file detection!
-R Detect whether the property is readable
-W Detect if it is a property that can be written
-X Detect if it is an executable property
-S Detection is "not a blank file"
-U Detect if there is a "SUID" property
-G Detect if there is a "SGID" property
-K Detect if a property with "sticky bit"
4. judgements and comparisons between two files ; for example [Test file1-nt file2]
-nt The first file is a new one than the second.
-ot The first file is older than the second file.
-ef The first file is the same file as the second file (link file)
5. Logical and (and), or (or)
&& The meaning of the logical and
|| The meaning of the logical OR

Operational symbols Representative meaning
= equals apply to: integer or string comparison if in [], it can only be a string
!= Not equal to: integer or string comparison if in [], it can only be a 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 apply to: integer comparison
-ne Not equal to: integer comparison
-lt Less than applied to: integer comparison
-gt Greater than applies to: integer comparison
-le Less than or equal to: integer comparison
-ge Greater-than or equal to: integer comparison
-A Both sides set up (and) logical Expressions –a logical expressions
-O Unilateral establishment (or) logical expression –o logical expression
-Z Empty string
-N Non-empty string

second, the logical expression

Test command

How to use: Test EXPRESSION

Such as:

Copy Code code as follows:

[root@localhost ~]# Test 1 = 1 && echo ' OK '
Ok
[Root@localhost ~]# test-d/etc/&& echo ' OK '
Ok
[Root@localhost ~]# Test 1-eq 1 && echo ' OK '
Ok
[Root@localhost ~]# if test 1 = 1; Then echo ' OK '; Fi
Ok


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

Thin expression

Copy Code code as follows:

[] An expression
[Root@localhost ~]# [1-eq 1] && echo ' OK '
Ok
[Root@localhost ~]# [2 < 1] && echo ' OK '
-bash:2: No such file or directory
[Root@localhost ~]# [2 \< 1] && echo ' OK '
[Root@localhost ~]# [2-gt 1-a 3-lt 4] && echo ' OK '
Ok
[Root@localhost ~]# [2-gt 1 && 3-lt 4] && echo ' OK '
-bash: [: Missing '] '

Note: In the [] expression, the common >,< needs to be preceded by an escape character that represents a string size comparison and a Acill code position. <> operator not directly supported, and logical operators | | && it needs to be represented by-a[and]–o[or]


[[]] An expression

Copy Code code as follows:

[Root@localhost ~]# [1-eq 1] && echo ' OK '
Ok
[Root@localhost ~]$ [[2 < 3]] && echo ' OK '
Ok
[Root@localhost ~]$ [2 < 3 && 4 > 5]] && echo ' OK '
Ok


Note: the [[]] operator is just an extension of the [] operator. The ability to support <,> symbolic operations does not require an escape character, it is also a string comparison size. Inside supports logical operators: | | &&

Third, performance comparison

Bash has three almost equivalent symbols and commands in the conditional expression: test,[] and [[]]. Usually, it is customary to use the form of if [];then. and [[]] the appearance, according to ABS, is to be compatible with the operators such as ><. The following is a comparison of their performance and found that [[]] is the fastest.

$ time (for M in {1..100000}; Done;)
Real 0m0.658s
User 0m0.558s
SYS 0m0.100s

$ time (for M in {1..100000}; does [-D.]; Done;)
Real 0m0.609s
User 0m0.524s
SYS 0m0.085s

$ time (for M in {1..100000}; does [[D.]]; Done;)
Real 0m0.311s
User 0m0.275s
SYS 0m0.036s

Without considering the compatibility of low version bash and SH, using [[]] is strong compatibility and performance is faster, you can use this operator when doing conditional operations.

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.