Shell basic condition judgment

Judge by document type

Test options

Test optionseffect
-bJudge whether the file exists and whether it is a block device file (yes, the block device file is true)
-cJudge whether the file exists and whether it is a character device file.
-dJudge whether the file exists and whether it is used as a directory file.
-eDetermine whether the file exists.
-fJudge whether the file exists and whether it is an ordinary file.
-LJudge whether the file exists and whether it is a symbolic link file.
-pJudge whether the file exists and whether it is a pipeline file.
-sJudge whether the file exists and is not empty.
-SDetermine 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 optionseffect
-rJudge whether the file exists and has read permission.
-wJudge whether the file exists and has write permission.
-xJudge whether the file exists and has execution permission.
-uJudge whether the file exists and has SUID permission.
-gJudge whether the file exists and has SGID permission.
-kJudge 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 optionseffect
File 1 -nt file 2Judge whether the modification time of file 1 is newer than that of file 2.
File 1 -ot file 2Judge whether the modification time of file 1 is older than that of file 2.
File 1 -ef file 2Judge 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 optionseffect
Integer 1 -eq integer 2Judge equality as true
Integer 1 -ne integer 2Unequal judgment is true
Integer 1 -gt integer 2Judge greater than as true
Integer 1 -lt integer 2Judge less than as true
Integer 1 -ge integer 2It is true to judge whether it is greater than or equal to
Integer 1 -le integer 2Judge 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 optionseffect
-z stringJudge whether the string is empty.
-n stringJudge whether the string is non empty.
String 1 = = string 2Judge whether the two strings are equal. Equal is true.
String 1= String 2Judge 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 optionseffect
Judgment 1 -a judgment 2When logic and, judgment 1 and judgment 2 are true, the result is true.
Judgment 1 -o judgment 2Logical or, when one of judgment 1 and judgment 2 is true, the final result is true.
! judgeLogical 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

Keywords: Linux bash

Added by Josh1billion on Sat, 05 Mar 2022 12:37:12 +0200