美文网首页
test condition

test condition

作者: zh_harry | 来源:发表于2018-10-22 19:05 被阅读0次

    test OR
    [
    test expr
    Evaluate a conditional expression expr and return a status of 0 (true) or 1 (false). Each operator and operand must be a separate argument. Expressions are composed of the primaries described below in Bash Conditional Expressions. test does not accept any options, nor does it accept and ignore an argument of -- as signifying the end of options.

    When the [ form is used, the last argument to the command must be a ].

    Expressions may be combined using the following operators, listed in decreasing order of precedence. The evaluation depends on the number of arguments; see below. Operator precedence is used when there are five or more arguments.

    ! expr
    True if expr is false.

    ( expr )
    Returns the value of expr. This may be used to override the normal precedence of operators.

    expr1 -a expr2
    True if both expr1 and expr2 are true.

    expr1 -o expr2
    True if either expr1 or expr2 is true.

    The test and [ builtins evaluate conditional expressions using a set of rules based on the number of arguments.

    0 arguments
    The expression is false.

    1 argument
    The expression is true if and only if the argument is not null.

    2 arguments
    If the first argument is ‘!’, the expression is true if and only if the second argument is null. If the first argument is one of the unary conditional operators (see Bash Conditional Expressions), the expression is true if the unary test is true. If the first argument is not a valid unary operator, the expression is false.

    3 arguments
    The following conditions are applied in the order listed. If the second argument is one of the binary conditional operators (see Bash Conditional Expressions), the result of the expression is the result of the binary test using the first and third arguments as operands. The ‘-a’ and ‘-o’ operators are considered binary operators when there are three arguments. If the first argument is ‘!’, the value is the negation of the two-argument test using the second and third arguments. If the first argument is exactly ‘(’ and the third argument is exactly ‘)’, the result is the one-argument test of the second argument. Otherwise, the expression is false.

    4 arguments
    If the first argument is ‘!’, the result is the negation of the three-argument expression composed of the remaining arguments. Otherwise, the expression is parsed and evaluated according to precedence using the rules listed above.

    5 or more arguments
    The expression is parsed and evaluated according to precedence using the rules listed above.

    When used with test or ‘[’, the ‘<’ and ‘>’ operators sort lexicographically using ASCII ordering.

    相关文章

      网友评论

          本文标题:test condition

          本文链接:https://www.haomeiwen.com/subject/lexnzftx.html