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
- [[Email protected] ~]# [-X Lnmp-a-F LNMP] #lmmp是可执行且是文件 results return True
- [[email protected] ~]# echo $?
- 0
- [[Email protected] ~]# [-X lnmp-a-D LNMP] #lnmp可执行但不是目录 results return False
- [[email protected] ~]# echo $?
- 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
- [[Email protected] ~]# [-X Lnmp-o-D LNMP] #lmmp是可执行但不是目录 results still return true
- [[email protected] ~]# echo $?
- 0
- [[Email protected] ~]# [-l lnmp-o-D Lnmp] #lnmp可执行文件, but not the directory, nor the symbolic link file, the result returns false
- [[email protected] ~]# echo $?
- 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
- [[Email protected] ~]# [! -X LNMP] #lnmp is an executable file that returns false
- [[email protected] ~]# echo $?
- 1
- [[Email protected] ~]# [! -D LNMP] #lnmp is not a directory, return True
- [[email protected] ~]# echo $?
- 0
Introduction to the Linux shell script logical operators