Shell condition test

Source: Internet
Author: User

 

Shell condition tests are usually used in control flow structures such as for, while, until, and if to determine the relationship between the object's related properties or variables.

Usage of conditional test: Test <expression>

Result: If the value is true, 0 is returned. If the value is not true, non-0 is returned.

View results: Echo $?

 

The following are some common test expressions:

1. 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: When filename
Returns true (or-l filename) If a symbolic link file exists)
-K pathname: When
Returns true if the specified file or directory exists and a "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: When filename exists and the file size is greater than 0
Returns true
-S filename: returns true if filename exists and is a socket
-T fd: When FD
Returns true if it 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: When
Returns true if the specified file or directory exists and is executable.
-O pathname: returns true if the pathname exists and is owned by the user ID of the current process.
Uppercase)
-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
: When file1 is greater than file2, true is returned.
File1-ot file2: file1 returns true when file1 is earlier than file2
Example: If [-B
/Dev/hda]; then Echo "yes"; else echo "no"; Fi // The result will print Yes;

Test-C/dev/hda; echo $? // The result will print 1; the return value of the test command is 1, because/dev/hda
Not a character device
[-W/etc/passwd]; echo $? // View passwd for the current user
File writable?

2. Use 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.

3. Test common strings
-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 the string in alphabetical order. str1 is prior to str2.
Str1> str2: sort the string in alphabetical order.
After
Example: name = "zqf"; [$ name = "zqf"]; echo $? // Print 0, indicating the variable name
Is equal to the string "zqf ".

4. 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
The value of variable X is equal to the number 1.
X = A; [$ X-EQ "1"] // Shell
Print 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.