test (Unix)
Test ( / bin / test ) is a Unix tool that can be employed with the logical comparisons. It belongs to the basic features of Unix-like systems and its behavior is standardized by the POSIX standard.
Many shells implement it now as a "built-in " command, but these implementations are all derived from the original version as a stand- alone program from.
From a usability reasons, the program also exists under the name of / bin / [, which behaves just like / bin / test, only that it as the last argument ] expected. See history.
Operation
According to convention of Unix utilities values of 0 as logically true values equal to 0 interpreted as false or wrong. test analyzes the simple or compound logical expression described in the passed arguments and assigns the result the same as the return value ( return code RC) 0 or 1. Since the branch operations of the shell scripting languages typically depend on the return value, so that is a generalized method to parameterize branches created.
The following application example, the do .. while loop is executed as long as the value of Counter is less than or equal to 100:
#! / bin / ksh typeset Counter = 0 while test $ counter -le 100; do ( (Counter = 1 ) ) done parameter
/ bin / test takes as arguments on the one hand values (strings, integers, and file name ), on the other hand, logical operators ( comparisons, conditions of existence of file metadata and logical links ) counter. From the truth value of a single or composite logic function is determined. The operators are divided into:
File -related operators
File operators expect a single path name as an argument.
It may be the existence of a file ( -e =
Integer operators
Integer operators expect two integer values as arguments, which are compared with each other according to the operator (
String operators
String operators expect two strings as arguments and check for equality or inequality ( "
Logical Operators
Several of the above conditions can be individual yet linked by a logical AND or OR together (
Operator precedence, grouping
It is implicitly the usual in other programming precedence logical operators, but also a different behavior, with the aid of grouping symbols ( \ (... \ )) are enforced. While this has the limitations of POSIX standards hardly any meaning, but the vast majority of real-world implementations, significantly more ( and for practical purposes, an infinite number ) of arguments as the spezifierten 4 process:
#! / bin / ksh typeset -i x = 0 typeset -i y = 1 typeset z = "" if test \ ($ x -gt 0 -a $ y ne 0 \ ) -o "$ z" = ""! ; then print - " ( false AND true) OR true - > true" fi if test $ x -gt 0 -a \ ( $ y - ne 0 -o "$ z" = "" \! ); then : else print - " false AND (true OR false ) -> false" fi history
The original design called for test, as it exists today. Since, however, so the appearance of shell programs differed markedly from that of other programming languages, test was made with a link as [available. This changed the appearance of scripts to:
#! / bin / ksh typeset Counter = 0 while [ $ counter -le 100; do ( (Counter = 1 ) ) done would have led to the somewhat unusual circumstance that a seemingly open-parenthesis is not closed. Therefore, the binary was changed so as to expect a final "]" as an argument when it is called as [. Thus, in today's conventional appearance was set in script languages . This is also the reason why [ and ] always spaces must be enclosed ( the Internal Field Separator Shell ):
#! / bin / ksh typeset Counter = 0 while [ $ counter -le 100]; do ( (Counter = 1 ) ) done Many of today's operating system versions replace the original link by a separate binary, but this does not affect the function. Many of today Unix shells have for performance reasons needs an identically functioning built-in, to be called so for a comparison no external software. For example, use the Korn shell and the Bourne - again shell [[, the logical ] ] expected as the final argument:
#! / bin / ksh typeset Counter = 0 while [ [$ counter -le 100 ] ]; do ( (Counter = 1 ) ) done Web Links
- Test ( 1) - Debian GNU / Linux Executable programs or shell commands manual page