首页
登录 | 注册

条件测试命令test和[ ]

    在实际工作中,多数程序会使用"[ ]","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
--------------------------------------------------------------------
 

相关文章

  • 初学者很多时候代码混乱,不是语法不熟,而是语义关系没理清,很多教参书只重语法而对语义关系缺乏应有的重视,所以就在这里稍作补充.比如if语句的条件关系. 格式一: if(a){ if(b){ } ... } 这种格式结构中,条件a与b是&qu ...
  •     Aspose.Cells for .NET 7.5.1包 括一个实用的功能,当把条件格式化添加到一个单元格或一系列单元格时,能调节图标集.数据棒或颜色测量对象.我们也添加了对在一个输出的HTML文件中渲 染一个工作表的背景的支持.该 ...
  • CU专家博主申请机制
    CU专家博主申请机制 CU博客在广大博主的帮助和支持下,正朝着健康的方向迅速发展,博主们发布的技术文章水平也越来越高,为了给CU博客平台上这些精英博主提供更好.更具针对性的服务,CU对获得较大影响力的技术类博客授予 "专家&quo ...
  • 1. 在C++中的详细说明 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库. vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象, 简单地说,vector是 ...
  • 性能优化方法和技巧
    系列目录 性能优化方法和技巧性能优化的方法和技巧:概述性能优化的方法和技巧:代码性能优化的方法和技巧:工具 这是一个可以用一本书来讲的话题,用一系列博客来讲,可能会比较单薄一点,这里只捡重要的说,忽略很多细节,当然以后还可以补充和扩展这个话 ...
  • Hadoop参数调优 1. 设置合理的槽位数目 在Hadoop中,计算资源是用槽位(slot)表示的.slot分为两种:Map slot和Reduce slot. 2. 调整心跳间隔  心跳中包含节点资源使用情况.各任务运行状态等信息,如果 ...

2020 unjeep.com webmaster#unjeep.com
12 q. 0.011 s.
京ICP备10005923号