[Reprinted] shell condition test

Source: Internet
Author: User

Original article address

: Http://blog.chinaunix.net/space.php? Uid = 1, 12077574



File status test
-B filename: returns true if filename exists and is a block file (0 is returned)
-C filename: returns true if filename exists and is a character file.
-D pathname: returns true if pathname exists and is a directory.
-E pathname: returns true if a file or directory specified by pathname exists.
-F filename: returns true if filename exists and is a regular file.
-G pathname: returns true if the file or directory specified by pathname exists and the sgid bit is set.
-H filename: returns true (or-l filename) If filename exists and is a symbolic link file)
-K pathname: returns the true value when the file or directory specified by pathname exists and the "Sticky" bit is set.
-P filename: returns true if filename exists and is a named pipe.
-R pathname: returns true if the file or directory specified by pathname exists and is readable.
-S filename: returns true if filename exists and the file size is greater than 0
-S filename: returns true if filename exists and is a socket
-T fd: returns true when FD is a file descriptor associated with the terminal device.
-U pathname: returns true if the file or directory specified by pathname exists and SUID bit is set.
-W pathname: returns true if a file or directory specified by pathname exists and can be written.
-X pathname: returns the true value when the file or directory specified by pathname exists and is executable.
-O pathname: returns true if the pathname exists and is owned by the user ID of the current process)
-G pathname: returns the true value when the user group of a user whose pathname exists and belongs to the valid user ID of the current process.
File1-nt file2: true when file1 is greater than file2
File1-ot file2: file1 returns true when file1 is earlier than file2
Example: If [-B/dev/hda]; then Echo "yes"; else echo "no"; Fi // print Yes
Test-C/dev/hda; echo $? // Print 1 to indicate that the return value of the test command is 1, And/dev/hda is not a character device.
[-W/etc/passwd]; echo $? // Check whether the passwd file is writable for the current user

Logical operators during testing
-Logic A and the operators are true on both sides, and the result is true. Otherwise, the result is false.
-O logic or, if either side of the operator is true, the result is true. Otherwise, the result is 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 can be written.

Common string tests
-Z string: returns true if string is an empty string (0 in length ).
-N string: returns true if the string is a non-empty string.
Str1 = str2: returns true if the string str1 is equal to the string str2
Str1! = Str2: returns true if str1 and str2 are not equal.
Str1 <str2: sort in alphabetical order. str1 is prior to str2.
Str1> str2: sort in alphabetical order. str1 is placed after str2.
Example: name = "zqf"; [$ name = "zqf"]; echo $? // Print 0 to indicate that the variable name value is equal to the string "zqf"

Common numerical tests
Int1-EQ int2: If int1 is equal to int2, true is returned.
Int1-ne int2: If int1 is not equal to int2, true is returned.
Int1-lt int2: If int1 is smaller than int2, true is returned.
Int1-Le int2: If int1 is smaller than or equal to int2, true is returned.
Int1-GT int2: If int1 is greater than int2, true is returned.
Int1-ge int2: If int1 is greater than or equal to int2, true is returned.
Example: x = 1; [$ X-EQ 1]; echo $? // Print 0 to indicate that the value of variable X is equal to the number 1
X = A; [$ X-EQ "1"] // The shell prints the error message [: A: integer expression expected

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.