在实际工作中,多数程序会使用"[ ]","test" 命令即shell的布尔判断命令. 在大多数系统上,这些命令的作用都差不多. 把"[ ]" 符号当作一条命令多少有点奇怪,但实际工作中,它的代码的确会使命令的语法看起来更简单明确.就像其他种类的程序设计语言一样.
在某些UNIX系统里,这些命令将调用shell中的一个外部程序,但比较现代的shell已经把它内建在其中了.
因为test命令在shell脚本程序以外用得不使很频繁,所以有些人把它忘了. 使用命令:
which test 查找命令test : /usr/bin/test . test在UNIX系统里提供的程序.
下面这2个程序是一样的功能:
-------------------------------
if test -f fred.c
then
statements
fi
-------------------------------
if [ -f fred.c ]
then
statements
fi
--------------------------------
如果习惯把then和if写在一行上,一定要用个分号把then和前面的语句分隔开.
比如:
if [ -f fred.c ]; then
statement
fi
-----------------------------------------
* 注意在方括号"[ ]" 和被检查的条件间要留出空格. 比如 [ condition ]
可以通过test命令进行测试的条件都可以归入下面的3类:
1. 字符串比较
-------------------------------------------------------------------
比较条件 结果
string1 = string2 如果2个字符串相同结果为真
string1 != string2 如果2个字符串不同结果为真
-n string 如果1个字符串不是空则结果为真
-z string 如果1个字符串是空结果为真
--------------------------------------------------------------------
2. 算术比较
--------------------------------------------------------------------
比较 结果
exp1 -eq exp2 如果2个表达式相等则结果为真
exp1 -ne exp2 如果2个表达式不等则结果为真
exp1 -gt exp2 如果前一个大于后一个结果为真
exp1 -ge exp2 如果前一个大于等于后一个则结果为真
exp1 -lt exp2 如果前一个小于后一个则结果为真
exp1 -le exp2 如果前一个小于等于后一个则结果为真
! exp 如果表达式为假则结果为真,表达式为真则结果为假
--------------------------------------------------------------------
3. 与文件有关的条件测试
--------------------------------------------------------------------
文件条件测试 结果
-d file 如果文件是一个子目录则结果为真
-e file 如果文件存在则结果为真
-f file 如果文件是一个普通文件则结果为真
-r file 如果文件可读则结果为真
更多内容在 man page里有详细信息
NAME
test - check file types and compare values
SYNOPSIS
test EXPRESSION
[ EXPRESSION ]
[ OPTION
DESCRIPTION
Exit with the status determined by EXPRESSION.
--help display this help and exit
--version
output version information and exit
EXPRESSION is true or false and sets exit status. It is one of:
( EXPRESSION )
EXPRESSION is true
! EXPRESSION
EXPRESSION is false
EXPRESSION1 -a EXPRESSION2
both EXPRESSION1 and EXPRESSION2 are true
EXPRESSION1 -o EXPRESSION2
either EXPRESSION1 or EXPRESSION2 is true
[-n] STRING
the length of STRING is nonzero
-z STRING
the length of STRING is zero
STRING1 = STRING2
the strings are equal
STRING1 != STRING2
the strings are not equal
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2
FILE1 is older than FILE2
-b FILE
FILE exists and is block special
-c FILE
FILE exists and is character special
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-h FILE
FILE exists and is a symbolic link (same as -L)
-G FILE
FILE exists and is owned by the effective group ID
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link (same as -h)
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and is readable
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t [FD]
file descriptor FD (stdout by default) is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
-w FILE
FILE exists and is writable
-x FILE
FILE exists and is executable
Beware that parentheses need to be escaped (e.g., by backslashes) for shells. INTEGER may also be -l STRING, which evalu-
ates to the length of STRING.
AUTHOR
Written by Kevin Braunsdorf and Matthew Bradburn.
REPORTING BUGS
Report bugs to <>.
COPYRIGHT
Copyright 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FIT-
NESS FOR A PARTICULAR PURPOSE.
SEE ALSO
The full documentation for [ is maintained as a Texinfo manual. If the info and [ programs are properly installed at your
site, the command
info coreutils test
should give you access to the complete manual.
---------------------------------------
16.3 `test'': Check file types and compare values
================================================
`test'' returns a status of 0 (true) or 1 (false) depending on the
evaluation of the conditional expression EXPR. Each part of the
expression must be a separate argument.
`test'' has file status checks, string operators, and numeric
comparison operators.
`test'' has an alternate form that uses opening and closing square
brackets instead a leading `test''. For example, instead of `test -d
/'', you can write `[ -d / ]''. The square brackets must be separate
arguments; for example, `[-d /]'' does not have the desired effect.
Since `test EXPR'' and `[ EXPR ]'' have the same meaning, only the former
form is discussed below.
Because most shells have a built-in command by the same name, using
the unadorned command name in a script or interactively may get you
different functionality than that described here.
Besides the options below, a single argument is also allowed: `test''
returns true if the argument is not null. The argument can be any
string, including strings like `-d'', `-1'', `--'', `--help'', and
`--version'' that most other programs would treat as options. To get
help and version information, invoke the commands `[ --help'' and `[
--version'', without the usual closing brackets. *Note Common options::.
Exit status:
0 if the expression is true,
1 if the expression is false,
2 if an error occurred.
* Menu:
* File type tests:: -[bcdfhLpSt]
* Access permission tests:: -[gkruwxOG]
* File characteristic tests:: -e -s -nt -ot -ef
* String tests:: -z -n = !=
* Numeric tests:: -eq -ne -lt -le -gt -ge
* Connectives for test:: ! -a -o
--------------------------------------------------------------------