Shell logical operations Summary, including [[]] and [] difference between,&& and-A, | | The difference from-O

Source: Internet
Author: User
Tags logical operators

1. about Files and directories

-F to determine if an ordinary file exists

-D to determine if a directory exists

-B Determine if a file is a block device

-C Determine if a file is a character device

-S to determine if a file is socket (pending correction)

-l Determine if a file is a symbolic link (pending correction)

-E To determine if something is present (pending correction)

-P to determine if a file is a pipe or FIFO

2. About the properties of a file

-R to determine if the file is a readable property

-W to determine if the file is a writable property

-X to determine whether the file is an executable property

-S to determine whether the file is a non-blank file

-U determines whether the file has suid properties

-G to determine if the file has Sgid properties

-K to determine if the file has a sticky bit attribute

3. Judgment and comparison 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 as the second file (link and other files)

4. Logical (and) and (or)

&& logical AND Meaning,-A is also the meaning of

|| Logical OR means,-O is also the meaning of

5. Operator-related

Operational symbols represent meaning

= equals applies To: integer or string comparison if in [], only the string

! = is 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: integer comparison

-le less than or equal to: integer comparison

-ge greater than or equal to: integer comparison

-A both are established (and) logical Expressions –a logical expressions

-O unilateral formation (or) logical expression –o logical expression

-Z Empty string

-N Non-empty string

6.1 Test Command

# test 1 = 1 && echo ' OK '

Ok

# test-d/etc/&& echo ' OK '

Ok

# test 1-eq 1 && echo ' OK '

Ok

# 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. Test 1=1 is wrong!

6.2 [] Expression

# [1-eq 1] && echo ' OK '

Ok

# [2 < 1] && echo ' OK '

-bash:2: No such file or directory

# [2 \< 1] && echo ' OK '

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

Ok

# [2-GT 1 && 3-lt 4] && echo ' OK '

-bash: [: Missing '] '

Note: In the [] expression, the common, < need to add an escape character, representing the string size comparison, Acill code position as a comparison.

Do not directly support <, > Operators, and logical operators | | , && it needs to be represented by-a[and]–o[or]

6.3 [[]] expressions

# [1-eq 1] && echo ' OK '

Ok

# [[2 < 3]] && echo ' OK '

Ok

#[[2 < 3 && 4 > 5]] && echo ' OK '

Ok

[[2 < 3-a 3 > 4]] && echo "OK"

-bash:syntax Error in conditional expression

-bash:syntax error near '-a '

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: | | &&, no longer using-a-o

Gorgeous split Line

*************************************************************************************************************** *******************************

Another part of the summary:

The difference between [[]] and [] in a bash shell

One, [[with "&&" instead of "-a" for logic "and", with "| |" Instead of "-O" means logical "or":

# [[1 < 2 && B > A]] && echo true | | echo False

True

# [[1 < 2-a B > A]] && echo true | | echo False

Bash:syntax Error in conditional expression

Bash:syntax error near '-a '

# [1 < 2-a B > A] && echo true | | echo False

True

# [1 < 2 && B > A]&& echo true | | echo false #wrong syntax

BASH: [: Missing '] '

False

Two, [...] As a shell command, so the expression in it should be its command-line argument, so the string comparison operator ">" and "<" must be escaped, otherwise it becomes an IO redirect operator. [Middle < and > do not need to be escaped:

# [2 \<] && echo true | | echo false #you should use "\<"

False


#[2-lt] && echo true | | echo False

True


# [[2 <]] && echo true | | echo False

False

Three, [[...]] Perform arithmetic expansion, while [...] Do not do.

# [[99+1-eq]] && echo true | | echo False

True

# [99+1-EQ] && echo true | | echo False

BASH: [: 99+1:integer expression expected

False

# [$ ((99+1))-eq] && echo true | | echo False

True

Four, [[]] can use the regular, and [] not

# ["test.php" = = *.php] && echo true | | echo False

False

# [["test.php" = = *.php]] && echo true | | echo False

True

# [["t.php" = = [a-z].php]] && echo true | | echo False

True

# ["test.php" = = "*.php"] && echo True | | echo False

False

# [["test.php" = = "*.php"]] && echo true | | echo False

False

Note: The effect of double quotation marks ("")


This article is from the "Cold River Exclusive Fishing" blog, please be sure to keep this source http://iter2012.blog.51cto.com/6873825/1613696

Shell logical operations Summary, including [[]] and [] difference between,&& and-A, | | The difference from-O

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.