Judge by document type
Test options
Test options | effect |
---|
-b | Judge whether the file exists and whether it is a block device file (yes, the block device file is true) |
-c | Judge whether the file exists and whether it is a character device file. |
-d | Judge whether the file exists and whether it is used as a directory file. |
-e | Determine whether the file exists. |
-f | Judge whether the file exists and whether it is an ordinary file. |
-L | Judge whether the file exists and whether it is a symbolic link file. |
-p | Judge whether the file exists and whether it is a pipeline file. |
-s | Judge whether the file exists and is not empty. |
-S | Determine whether the file exists and whether it is a socket file. |
example
- Determine whether the file exists
[root@localhost ~]# test -e test.txt
[root@localhost ~]# echo $?
0
#The return value is 0, indicating that there is a problem
- Use "[]" instead of the test command. Generally, brackets are used in use, because the if command does not recognize the test command
[root@localhost ~]# [ -e test.txt ]
[root@localhost ~]# echo $?
0
- Use echo to output true and false directly
[root@localhost ~]# [ -e test.txt ] && echo yes || echo no
yes
[root@localhost ~]# [ -e test.tx ] && echo yes || echo no
no
Judge according to file permissions
Test options
Test options | effect |
---|
-r | Judge whether the file exists and has read permission. |
-w | Judge whether the file exists and has write permission. |
-x | Judge whether the file exists and has execution permission. |
-u | Judge whether the file exists and has SUID permission. |
-g | Judge whether the file exists and has SGID permission. |
-k | Judge whether the file exists and whether it has SBit permission. |
example
- Judge whether the file has permission to read
[root@localhost ~]# [ -r mail ] && echo yes || echo no
yes
- Judge whether the file has SUID permission
[root@localhost ~]# [ -u mail ] && echo yes || echo no
no
Compare between two files
Test options
Test options | effect |
---|
File 1 -nt file 2 | Judge whether the modification time of file 1 is newer than that of file 2. |
File 1 -ot file 2 | Judge whether the modification time of file 1 is older than that of file 2. |
File 1 -ef file 2 | Judge whether the Inode number of file 1 is consistent with that of file 2, which can be used to judge the hard link. |
example
- Judge the difference between two files
[root@localhost ~]# [ mail -nt test.txt ] && echo yes || echo no
no
[root@localhost ~]# [ mail -ot test.txt ] && echo yes || echo no
yes
- Determine whether the two files are hard links
[root@localhost ~]# ln mail mail2
[root@localhost ~]# ls
anaconda-ks.cfg mail mail2 photo sh test.txt
[root@localhost ~]# [ mail -ef mail2 ] && echo yes || echo no
yes
Comparison between two integers
Test options
Test options | effect |
---|
Integer 1 -eq integer 2 | Judge equality as true |
Integer 1 -ne integer 2 | Unequal judgment is true |
Integer 1 -gt integer 2 | Judge greater than as true |
Integer 1 -lt integer 2 | Judge less than as true |
Integer 1 -ge integer 2 | It is true to judge whether it is greater than or equal to |
Integer 1 -le integer 2 | Judge whether less than or equal to true |
example
- Judge whether two numbers are equal
[root@localhost ~]# [ 20 -eq 21 ] && echo yes || echo no
no
- Judge whether integer 1 is less than integer 2
[root@localhost ~]# [ 20 -lt 21 ] && echo yes || echo no
yes
String judgment
Test options
Test options | effect |
---|
-z string | Judge whether the string is empty. |
-n string | Judge whether the string is non empty. |
String 1 = = string 2 | Judge whether the two strings are equal. Equal is true. |
String 1= String 2 | Judge whether two strings are not equal to true. |
Note: you can only judge whether the string is equal in the shell.
example
- Judge whether it is empty.
[root@localhost ~]# wjl=""
[root@localhost ~]# [ -z "$wjl" ] && echo yes || echo no
yes
Multiple conditional judgment
Test options
Test options | effect |
---|
Judgment 1 -a judgment 2 | When logic and, judgment 1 and judgment 2 are true, the result is true. |
Judgment 1 -o judgment 2 | Logical or, when one of judgment 1 and judgment 2 is true, the final result is true. |
! judge | Logical negation makes the judgment result negative. |
example
- True when the value of the variable is not empty and greater than 18
[root@localhost ~]# [ -n "$wjl" -a "$wjl" -gt 18 ] && echo yes || echo no
yes
ps: Shang Silicon Valley linux video course Study notes