Shell conditional judgment statement

Conditional judgment

1. Judging according to the file type process

Test options effect
-b file Determine whether the changed file exists, and whether it is a block device file (the block device file is true)
-c file Determine whether the changed file exists and whether it is a character device file (true if it is)
(Commonly used) -d file Determine whether the changed file exists and whether it is a directory file (true if it is)
(Commonly used) -e file Determine whether the changed file exists (existing is true)
(Commonly used) -f file Determine whether the modified file exists and whether it is a normal file (true if it is)
-L file Determine whether the changed file exists and whether it is a symbolic link file (true if it is)
-p file Determine whether the changed file exists and whether it is a pipeline file (true if it is)
-s file Determine whether the changed file exists and whether it is empty (true if it is)
-S file Determine whether the changed file exists and whether it is a socket file (true if it is)

2. Judge according to file permissions

Test options effect
(Commonly used) -r file Determine whether the changed file exists, and whether the file has read permission (true if there is)
(Commonly used) -w file Determine whether the modified file exists, and whether the file has write permission (true if there is)
(Commonly used) -x file Determine whether the changed file exists, and whether the file has execution permission (true if there is)
-u file Determine whether the changed file exists, and whether the file has SUID permission (true if there is)
-g file Determine whether the changed file exists, and whether the file has SGID permissions (true if there is)
-k file Determine whether the changed file exists, and whether the file has SBit permission (true if there is)

3. Compare between two files

Test options effect
File a -nt file b Determine whether the modification time of file a is newer than that of file b (true if it is new)
File a -ot file b Determine whether the modification time of file a is older than that of file b (true if it is old)
File a -ef file b Judge whether the Inode number of file a and file b is the same, you can understand whether the two files are the same file. This is a good way to judge hard links
  • ps: soft link, delete the original |, soft link is also deleted. Hard link, the Inode numbers of the two files are the same

4. Comparison between two integers

Test options effect
Integer a -eq integer b Determine whether the integer a is equal to the integer b (equal is true)
Integer a -ne integer b Determine whether the integer a and the integer b are not equal (not equal to true)
Integer a -gt integer b Determine whether the integer a is greater than the integer b (greater than is true)
Integer a -lt integer b Determine whether the integer a is less than the integer b (less than is true)
Integer a -ge integer b Determine whether the integer a is greater than or equal to the integer b (greater than or equal to true)
Integer a -le integer b Determine whether the integer a is less than or equal to the integer b (less than or equal to true)

5. Judgment of string

Test options effect
-z string Determine whether the string is empty (return true if it is empty)
-n string Determine whether the string is non-empty (non-empty returns true)
String a == string b Determine whether the string a is equal to the string b (equal to return true)
String a! = String b Determine whether the string a and the string b are not equal (not equal to return true)

6. Multiple condition judgment

Test options effect
Judge a -a judge b Logical AND, judgment a and judgment b are both established, the final result is true
Judge a -o judge b Logical OR, if one of judgment a and judgment b is established, the final result is true
! judgment Logic negation, reverse the original judgment

7. Two judgment formats

test 参数 文件路径			文件判断

[ 参数 文件路径 ]				文件判断

[ 整数a 参数 整数b ]			两数之间判断

[ 参数 “$变量名” ]			判断字符串
Ps:	[ -d /root ] && echo "yes" || echo "no"

	&&	左边为真则执行右边

	||	左边为否则执行右边

	[ ]括号要留空,任何数,变量,不能紧贴括号

	$变量名 	代表值	变量名	代表变量

Guess you like

Origin blog.csdn.net/weixin_43272542/article/details/109553283