if conditional expression

Commonly used:
[-a FILE] True if FILE exists.
[-D FILE] FILE exists and if it is a directory returns true.
[-E FILE] returns true if the specified file or directory exists.
[-F FILE] If FILE exists and is a regular file returns true.
[-R & lt FILE] If FILE exists and is readable returns true.
[-W FILE] If FILE exists and is writable returns true. (A directory for its contents to be accessed must be executable)
[-x FILE] If FILE exists and is executable returns true.

Conditional expression significance
1. File class detection, e.g. [-e /tmp/a.log]. If not otherwise specified, all files class action soft links are traced to the source file.
-e file If a file exists (exist)
-f file If a file exists and is a regular file (file)
-d file If a file exists and is a directory (directory)
-b file If the file exists and block device to a block device
-c file If a file exists and is a character device character device
-S file If the file exists and is a socket file Socket
-p file If a file exists and is a named pipe file FIFO (pipe)
-L file If the file exists and is a file link (Link)
2. File attribute detection, such as test -r /tmp/a.log. If not otherwise specified, all files class action soft links are traced to the source file.
-r file If the file exists and the current user-readable
-w file If the file exists and the current user can write
-x file If the file exists and the current user can perform
-u file If the file exists and its SUID
-g file If the file exists and its SGID
-k file If the file exists and its sbit (sticky bit)
-s file If the file size is greater than 0 exists and bytes, i.e., for detecting whether the file is not empty files
-N file If the file exists, and since it was last read whether modify
3. Comparison between the two files, such as: test file1 -nt file2
-Nt file1 file2 If (newer than) than file1 file2 new judge
file1 file2 -ot If (older than) judgment file1 is older than file2
-If file1 file2 (Equal file) and file2 file2 determines whether the same file, can be used in determining the hard link is determined. The main significance in judging whether two files point to the same inode on the same partition
4. determined between two integers, to support positive and negative numbers, but not decimal. For example test n1 -eq n2
int1 -eq int2 Two values ​​are equal (equal)
int1 -ne int2 Two numerical ranges (not equal)
int1 -gt int2 n1 is greater than n2 (greater than)
int1 -lt int2 n1 is smaller than n2 (less than)
int1 -ge int2 greater than or equal n1 n2 (greater than or equal)
-The int1 int2 less n1 n2 (less than or equal)
5. determines that the string
-z string (Zero) to determine whether the string is empty? If the string is an empty string (true)
-n string Determine whether the string is not empty? If the string is a null string, then false. NOTE: -n may be omitted
string1 = string2 string1 == string2 string1 and string2 are the same. The same as it returns true. "==" and "=" is equivalent, but the "=" better portability
str1! = str2 str1 is not equal to str2, if not, etc., then return true
str1 > str2 Alphabetical str1 is greater than str2, if more than, returns true
str1 < str2 str1字母顺序是否小于str2,若小于,则返回true
6.逻辑运算符,例如:test -r filename -a -x filename
-a或&& (and)两表达式同时为true时才为true。"-a"只能在test或[]中使用,&&只能在[[]]中使用
-o或|| (or)两表达式任何一个true则为true。"-o"只能在test或[]中使用,||只能在[[]]中使用
[ ! EXPR ] 对表达式取反
( ) 用于改变表达式的优先级,为了防止被shell解析,应该加上反斜线转义\(\)
[ ] || [ ] 用OR来合并两个条件
[ ] && [ ] 用AND来合并两个条件

忘记参考的哪儿了,请见谅

Guess you like

Origin www.cnblogs.com/qiaozhuangshi/p/11762532.html