Shell condition Test

Source: Internet
Author: User

File Status Test
-B FileName: Returns True when filename exists and is a block file (returns 0)
-C FileName: Returns True when filename exists and is a character file
-D Pathname: Returns True when pathname exists and is a directory
-E Pathname: Returns True when a file or directory specified by pathname is present
-F FileName: Returns True when filename exists and is a regular file
-G Pathname: Returns True when the file or directory specified by pathname is present and the Sgid bit is set
-H FileName: Returns True when filename exists and is a symbolic link file (or-l filename)
-K Pathname: Returns True when a file or directory specified by pathname exists and the "sticky" bit is set
-P FileName: Returns True when filename exists and is named pipe
-R Pathname: Returns True when a file or directory specified by pathname exists and is readable
-S FileName: Returns True when filename exists and the file size is greater than 0 o'clock
-S FileName: Returns True when filename exists and is a socket
-T FD: Returns True when FD is a file descriptor associated with an end device
-U Pathname: Returns True when the file or directory specified by pathname is present and the SUID bit is set
-W Pathname: Returns True when a file or directory specified by pathname exists and is writable
-X Pathname: Returns True when a file or directory specified by pathname exists and is executable
-O Pathname: Returns True when a user with a valid user ID of the current process exists and is owned by pathname (letter O uppercase)
-G Pathname: Returns True when the user group of a user that is present by pathname and belongs to the current process's valid user ID
File1-nt file2:file1 than File2 new when return True
File1-ot file2:file1 than file2 back true
Example: if [-b/dev/hda]; then echo "yes"; else echo "no"; fi//will print Yes
Test-c/dev/hda; echo $? Will print 1 to indicate that the return value of the test command is 1,/DEV/HDA not a character device
[-W/ETC/PASSWD]; echo $? See if the passwd file is writable for the current user

Test-time logical operator
-a logic with, both sides of the operator are true, the result is true, otherwise false.
-O logic or, the operator is true on either side, the result is true, otherwise false.
! Logical No, the condition is false and the result is true.
Example: [-w Result.txt-a-W Score.txt]; echo $? Test whether two files are writable

Common string tests
-Z String: strings string is a null string (length 0) returns True
-N String: Strings string is a non-empty string when returning true
STR1 = str2: Returns True when string str1 and string str2 are equal
Str1! = str2: Returns True when string str1 and string str2 are not equal
STR1 < str2: sorted by dictionary order, string str1 before string str2
str1 > str2: Sorted in dictionary order, string str1 after string str2
Example: Name= "ZQF"; [$name = "ZQF"];echo $? Printing 0 means the value of the variable name and the string "ZQF" are equal

Common numerical tests
Int1-eq int2: If Int1 equals Int2, returns True
Int1-ne Int2: If int1 is not equal to Int2, returns True
Int1-lt Int2: If Int1 is less than Int2, returns true
Int1-le Int2: Returns True if Int1 is less than or equal to Int2
INT1-GT Int2: If Int1 is greater than Int2, returns true
Int1-ge Int2: Returns True if Int1 is greater than or equal to Int2
Example: x=1; [$x-eq 1]; echo $? Printing 0 means that the value of the variable x equals the number 1
X=a; [$x-eq "1"]//Shell print error message [: A:integer expression expected

Posted from: http://blog.chinaunix.net/u/21352/showart_135230.html]
posted from:http://www.blogjava.net/jasmine214--love/archive/2011/02/09/343963.html

Shell condition Test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.