Description of Shell test condition expression in Linux

Source: Internet
Author: User
Tags arithmetic bit set builtin locale readable sorts valid

Conditional expression ("CONDITIONAL EXPRESSIONS")

Conditional expressions are used in [composite commands and built-in command test and [to test file properties for string and arithmetic comparisons.] An expression is constructed using the following single or binary operation. If any of the file parameters of an operation are in the form of
File symbols:


At the beginning of the section "6.4 Bash Conditional Expressions" in Info bash, there is a sentence that illustrates two mechanisms of shell condition judgment:

Conditional expressions are used by the ' [' Compound command and the
' Test ' and ' [' Builtin commands.

Mechanism one: built-in (BUILTIN) function test (operator []), a complete description of the "4.1 Bourne Shell Builtins"

Mechanism two: [[]], the conditional structure key word, concrete can view "3.2.4.2 Conditional constructs"

Test is the same as [], but one in the form of a function, one in the form of a pair of operators,

Test contains the expression form of the 6.4-section conditional expression,

Is the function has the parameter, test to 1, 2, 3 ... 6 The judgment of multiple parameters is defined separately

and [[]], most of the functions are the same as [], can be simply understood as an extension of [], but for the definition of parameters, more complete definition than test, not easy to appear in [] Error

For example, in the right-hand operator of-eq comparison, [] requires that the right operator is not empty, otherwise the error returns 2, and [[]] does not:

[Root@localhost tmp]# [1-eq $a]
+ ' [' 1-eq '] '
-bash: [: 1:unary operator expected
[Root@localhost tmp]# echo $?
+ Echo 2
2
[Root@localhost tmp]# [[1-eq $a]]
+ [[1-eq ']]
[Root@localhost tmp]# echo $?
+ Echo 1
1

This is also a lot of programmers like to use [[]] reason, not error prone

[[]] expands two features compared to test's ability to express

1:pattern match, wildcard matching, using double equals "="

2:regex match, regular matching, using the "=~" operation symbol

First, the following wildcard matches are described:

First explain [] in the = = operation, [] also includes = = comparison, for string equality comparison

Similarly, the = = number here also does not allow the right operator to be empty, otherwise the error

[Root@localhost tmp]# ["abc" = $a]
+ ' [' abc = = '] '
-bash: [: abc:unary operator expected

The solution is to add a quote

[Root@localhost tmp]# ["abc" = "$a"]
+ ' [' abc = = '] '

and [[]] = =, in 3.2.4.2 has introduced

When the ' = = ' and '!= ' operators are used, the string to the right
The operator is considered a pattern and matched according to
The rules described below in *note pattern Matching:: ...

Any of the "the pattern" may quoted
To force it is matched as a string.

At least two points to note

1:[[]] = = operator, all the right operators are regarded as a pattern,

= = In fact, not just a simple string comparison, but pattern match, that is, wildcard matching, the specific rules can refer to

3.5.8.1 pattern Matching

2: The last sentence: quotes can directly define the string, in 3.5.8.1 There is also a sentence: the special pattern
Characters must is quoted if they are to be matched literally.

That is, the pattern special character is the character in quotation marks to remove the special definition

This also explains the difference between the two examples below:

[Root@localhost tmp]# Set-x
+ set-x
[Root@localhost tmp]# [ABC = = A*c]]
+ [[ABC = a*c]]
[Root@localhost tmp]# echo $?
+ Echo 0
0
[Root@localhost tmp]# [ABC = "A*C"]]
+ [[ABC =/A/*/C]]
[Root@localhost tmp]# echo $?
+ Echo 1
1

Also need to be aware of:

in [[, the document mentions

Word splitting and filename expansion are not
Performed on the words between the ' [[' and ']] '; Tilde expansion,
Parameter and variable expansion, arithmetic expansion, command
Substitution, process substitution, and quote removal are
performed.


That is, the word split word splitting and filename extension filename expansion in [[] without bash command resolution
But it did "~ symbol extension, parameter and variable extension, arithmetic extension, command substitution, process substitution, quotation mark removal"

So you don't have to worry about whether A*b will expand to the file name in the directory above.

As for the =~ matching, there is nothing in particular, the right operand directly defined as a regular formula that is, where the regular quotation marks can not be used (can not be used/)

Because in the "quote removal" link, the extension of the quotation marks are removed, in fact, with quotes and without quotes is the same result

[Root@localhost tmp]# [ABC =~ ' AB*C ']]
+ [[ABC =~ AB*C]]
[Root@localhost tmp]# echo $?
+ Echo 0
0
[Root@localhost tmp]# [ABC =~ "AB*C"]]
+ [[ABC =~ AB*C]]
[Root@localhost tmp]# echo $?
+ Echo 0
0
[Root@localhost tmp]# [[ABC =~ AB*C]]
+ [[ABC =~ AB*C]]
[Root@localhost tmp]# echo $?
+ Echo 0
0
[Root@localhost tmp]# [[ABC =~/ab*c/]]
+ [[ABC =~/ab*c/]]
[Root@localhost tmp]# echo $?
+ Echo 1
1

Example

[Root@lamp test]# Help test
test:test [expr]
Evaluate conditional expression.
Exits with a status of 0 (True) or 1 (false) depending on
The evaluation of EXPR.  Expressions May is unary or binary. Unary
Expressions are often used to examine the status of a file. There
are string operators as, and numeric comparison operators.

File operators:

-A file True if FILE exists.
-B file True if FILE is block special.
-C file True if FILE is character special.
-D file True If FILE is a directory.
-e file True if file exists.
-F File True If file exists and is a regular file.
-G file True if FILE is Set-group-id.
-H file True If FILE is a symbolic link.
-L file True if FILE is a symbolic link.
-k file True if file has its ' sticky ' bit set.
-P File True if file is a named pipe.
-R file True if FILE is readable by you.
-S FILE True if file exists and is not empty.
-S file True if FILE is a socket.
-T FD True if FD is opened on a terminal.
-U file True If the FILE is Set-user-id.
-W FILE True If the file is writable by you.
-X FILE True If the file is executable by you.
-O FILE True If the FILE is effectively owned by you.
-G file True If the file is effectively owned by your group.
-N FILE True If the file has been modified since it is last read.

File1-nt FILE2 True If file1 is newer than file2 (according to
Modification date).

File1-ot FILE2 True If file1 is older than file2.

File1-ef FILE2 True If file1 is a hard link to file2.

String operators:

-Z STRING True if STRING is empty.

-N STRING
String True If string is not empty.

STRING1 = STRING2
True if the strings are equal.
STRING1!= STRING2
True if the strings are not equal.
STRING1 < STRING2
True if STRING1 sorts before STRING2 lexicographically.
STRING1 > STRING2
True if STRING1 sorts after STRING2 lexicographically.

Other operators:

The-o option True if the shell option is enabled.
! EXPR True If expr is false.
Expr1-a EXPR2 True if both Expr1 and EXPR2 are true.
Expr1-o EXPR2 True if either Expr1 OR expr2 is True.

Arg1 OP arg2 arithmetic tests. OP is one of-eq,-ne,
-lt,-le,-GT, Or-ge.

Arithmetic binary operators return true if ARG1 is equal, not-equal,
Less-than, Less-than-or-equal, Greater-than, or greater-than-or-equal
than ARG2.

Exit Status:
Returns Success If EXPR evaluates to true; Fails if EXPR evaluates to
False or a invalid argument is given.
-A file

Related reference


True if file exists.
-B File
True if file exists and is a block device.
-C file
True if file exists and is a character device.
-D File
True if file exists and is a directory.
-E File
True if file exists.
-F File
True if file exists and is normal.
-G file
True if file exists and is the set group ID (sgid).
-H file
True if file exists and is a symbolic link.
-K File
True if file exists and the ' sticky ' bit (sticky bit) is set.
-P File
True if file exists and is a named pipe (FIFO).
-R File
True if file exists and is readable.
-S file
True if file exists and the size is greater than zero.
-T FD if the file descriptor FD is open and a corresponding terminal is true.
-U file
True if file exists and is set to the user ID (suid).
-W File
True if file exists and is writable.
-X File
True if file exists and is executable.
-O File
True if file exists and is owned by a valid user ID.
-G file
True if file exists and is owned by a valid group ID.
-L File
True if file exists and is a symbolic link.
-S file
True if file exists and is a socket.
-N File
True if file exists and has been modified since it was last read.
File1-nt File2
True if File1 is newer than file2 (based on modified date), or if file1 exists and file2 does not exist.
File1-ot File2
True if the file1 is older than file2, or if file1 does not exist and file2 exists.
File1-ef File2
True if File1 and File2 refer to the same device and inode number.
-O optname
True if the shell option is enabled optname. See the list of options in the description of the-O option under the built-in command set.
-Z String
True if string has a length of 0.
-N String
String if the length of string is not 0 is true.
string1 = = string2
True if the string is equal. = can be used to compatibility with the POSIX specification on the occasion of = =.
String1!= string2
True if the string is not equal.
String1 < string2
True if string1 is before string2 in the dictionary order of the current locale.
String1 > string2
True if string1 is after string2 in the dictionary order of the current locale.
Arg1 OP arg2
OP is one of-eq,-ne,-lt,-le,-GT, or-ge.  These arithmetic binary operations return TRUE if the arg1 and arg2 are equal, unequal, less than, less than or equal to, greater than, greater than or equal to the relationship. Arg1 and
ARG2 can be positive/negative integers.

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.