Shell script syntax-conditional test: test or [command

Source: Internet
Author: User
Article from:

Http://learn.akae.cn/media/ch31s05.html


Conditional test for shell script Syntax: test or [

Commandtest
Or[
You can test whether a condition is true. If the test result is true, the exit status of the command is 0. If the test result is false, the exit status of the command is 1 (note that the logical representation of the C language is exactly the opposite ). For example, test the relationship between two numbers:

$ VAR=2
$ test $VAR -gt 1
$ echo $?
0
$ test $VAR -gt 3
$ echo $?
1
$ [ $VAR -gt 3 ]
$ echo $?
1

Although it looks strange, the left square brackets[
It is indeed a command name. The parameters passed to the command should be separated by spaces.


, For example,$VAR
,-gt
,3
,]
Yes[
Command, which must be separated by spaces. Commandtest
Or[
The parameter format is the same,test
Command not required]
Parameters. To[
For example, the following table lists common test commands:

Table 31.2. Test commands

[ -d DIR ] IfDIR
If a directory exists and is true
[ -f FILE ] IfFILE
If a common file exists, it is true.
[ -z STRING ] IfSTRING
The length of 0 is true.
[ -n STRING ] IfSTRING
The non-zero length is true.
[ STRING1 = STRING2 ] True if the two strings are the same
[ STRING1 != STRING2 ] True if the strings are different
[ ARG1 OP ARG2 ] ARG1
AndARG2
It should be an integer or a variable with an integer value,OP
Yes-eq
(Equal)-ne
(Not equal)-lt
(Less)-le
(Less than or equal)-gt
(Greater)-ge
(Greater than or equal)


Similar to the C language, the test conditions can also be compared with, or, non-logical operations:

Table 31.3. Test commands with and, or, not

[ ! EXPR ] EXPR
It can be any test condition in the preceding table ,! Logical Inversion
[ EXPR1 -a EXPR2 ] EXPR1
AndEXPR2
It can be any test condition in the preceding table,-a
Representation logic and
[ EXPR1 -o EXPR2 ] EXPR1
AndEXPR2
It can be any test condition in the preceding table,-o
Indicates logic or


For example:

$ VAR=abc
$ [ -d Desktop -a $VAR = 'abc' ]
$ echo $?
0

Note: If$VAR
If the variable is not defined in advance, the shell expands to an empty string, causing a syntax error in the test condition (expanded[ -d Desktop -a = 'abc' ]
As a good shell programming habit, the variable value should always be placed in double quotation marks (expanded[ -d Desktop -a "" = 'abc' ]
):

$ unset VAR
$ [ -d Desktop -a $VAR = 'abc' ]
bash: [: too many arguments
$ [ -d Desktop -a "$VAR" = 'abc' ]
$ echo $?
1


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.