Linux Shell condition test

Source: Internet
Author: User
Tags builtin
ArticleDirectory
    • File status test

1. File condition test
-R readable
-W writable
-X executable
-D directory
-F: General Files
-Block B Devices
-C character device
-U file SUID
-L Symbolic Link
-S file is greater than 0, not empty
-E: whether the file exists

2. String condition test
= The two strings are the same
! = Two strings are different
-Z is an empty string
-N non-empty string

3. Numerical condition test
-Equal EQ
-Ne is not equal
-GT greater
-Ge is greater than or equal
-Lt is less
-Le is less than or equal

── ─ ── ─

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 If the file or directory specified by pathname exists and the sgid bit is set, the system returns the true
-H filename Returns true (or-l filename) When filename exists and is a symbolic link file)
-K pathname If the file or directory specified by pathname exists and the "Sticky" bit is set, the system returns the true
-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 if FD is a file descriptor associated with the terminal device.
-U pathname If a file or directory specified by pathname exists and SUID bit is set, the system returns true
-W pathname Returns true if a file or directory specified by pathname exists and can be written.
-X pathname Returns true if the file or directory specified by pathname exists and is executable.
-O pathname Returns true (uppercase letter O) when the pathname exists and is owned by a user with a valid user ID of the current process)
-G pathname If 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 better than file2, true is returned.
File1-ot file2 If file1 is earlier than file2, true is returned.
F1-Ef F2 Files F1 and F2 are hard links to the same file

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 and, the operator is true on both sides, the result is true, otherwise it 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 Same as =
Str1! = Str2 Returns true if str1 and str2 are not equal.
Str1 <str2 Sort by dictionary. The string str1 is prior to the string str2.
Str1> str2 Sort by dictionary, and the string str1 is after the string 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 less 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.

Test in:

< Less than (used in double brackets) ("$ A" <"$ B "))
<= Less than or equal to (used in double brackets) ("$ A" <= "$ B "))
> Greater than (used in double brackets) ("$ A"> "$ B "))
> = Greater than or equal to (used in double brackets) ("$ A"> = "$ B "))

Example: x = 1; [$ X-EQ 1]; echo $? // Print 0 to indicate that the value of variable X is equal to the number 1X = A; [$ X-EQ "1"] // The shell prints the error message [: A: integer expression expected

Test, [], [[]

Because shell and we usuallyProgramming LanguageDifferent, it is more often to interact with it and always call others. So some belongProgramThe concept of language itself is hard to understand in shell. "Basic Skills" are not good, and it is easier to be "sleepy". I am just one.

Take Bash as an example (other compatible with Shell ):

    1. Test and [are Bash Internal commands. The coreutils software package of the GNU/Linux system usually includes the/usr/bin/test and/usr/bin/[commands. If we do not need to specify the absolute path, we usually use Bash commands.
    2. [[It is a keyword of bash programming language!
 
$ LS-L/usr/bin/[/usr/bin/test
-Rwxr-XR-x 1 Root 37400 September 18 15:25/usr/bin /[
-Rwxr-XR-x 1 Root 33920 September 18 15:25/usr/bin/test
$ Type [[[Test
[Is a shell builtin
[Is a shell keyword
Test is a shell builtin

In most cases, these three functions are common. However, commands and keywords are always different. What is the difference between commands and keywords?

If it is a command, it is interpreted by shell as a combination of parameters. For example, ">" <"is interpreted as a redirection symbol by shell. The keyword is not like this.

Use & and | in [

[Use-A and-O to represent the logic and logic or.

[Wildcard characters can be used in [
 
Arch = istrap
[[$ Arch = I * 86] & Echo "arch is x86! "
[[Match strings or wildcards without quotation marks
 [[$ arch_com = i386 | $ arch = I * 86] & 
CAT> $ tfs_repo [tfs-i386]
name = gtes11.3 prelim1
baseurl =$ {baseurl} i386/
enabled = 1
EOF
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.