Introduction to the Linux shell script logical operators

Source: Internet
Author: User
Tags logical operators

When writing a program, the condition is used to determine whether the test condition is established. Many times, the judgment condition is multiple, this time needs to use the logical operator. What are the logical operators that are commonly used in shell scripts?

1, logic and:-A

Format: Conditon1-a condition2

Result: Codition1 and Conditon2 are true, only return true, otherwise return false.

Demo: Test the executable file Lnmp

[Plain] view plaincopy

  1. [[Email protected] ~]# [-X Lnmp-a-F LNMP] #lmmp是可执行且是文件 results return True
  2. [[email protected] ~]# echo $?
  3. 0
  4. [[Email protected] ~]# [-X lnmp-a-D LNMP] #lnmp可执行但不是目录 results return False
  5. [[email protected] ~]# echo $?
  6. 1

Expansion: This operator is similar to && in other languages, A is the first letter of and

2, logic or:-O

Format: Condition1-o conditon2

Results: In Condition1 and condition2, one condition is true, which returns true, otherwise false

Demo: Test the executable file Lnmp

[Plain] view plaincopy

  1. [[Email protected] ~]# [-X Lnmp-o-D LNMP] #lmmp是可执行但不是目录 results still return true
  2. [[email protected] ~]# echo $?
  3. 0
  4. [[Email protected] ~]# [-l lnmp-o-D Lnmp] #lnmp可执行文件, but not the directory, nor the symbolic link file, the result returns false
  5. [[email protected] ~]# echo $?
  6. 1

Expand: This action is similar to other languages | |

Reminder:-O is the first letter o of the word or, not the number 0

3, logical non-:!

Format:! Condition

Result: Returns the opposite value of the condition test condition result

Demo: Test the executable file Lnmp

[plain] view plaincopy
  1. [[Email protected] ~]# [! -X LNMP] #lnmp is an executable file that returns false
  2. [[email protected] ~]# echo $?
  3. 1
  4. [[Email protected] ~]# [! -D LNMP] #lnmp is not a directory, return True
  5. [[email protected] ~]# echo $?
  6. 0

Introduction to the Linux shell script logical operators

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.