Test (Unix)

Test (Unix)

test is a Unix command that evaluates conditional expressions.

yntax

test "expression"or [ "expression" ]

Description

The test command evaluates the expression parameter. In the second form of the command, the [ ] (brackets) must be surrounded by blank spaces. You must test explicitly for file names in the C shell. File-name substitution (globbing) causes the shell script to exit. Functions and operators are treated as separate parameters by the test command. The expression parameter refers to a statement that is checked for a true or false condition.

Functions

The following functions are used to construct this parameter:

-b Filename - Returns a True exit value if the specified FileName exists and is a block special file.

Note: All remaining functions return true if the object (file or string) exists, and the condition specified is true. -c FileName - FileName is a character special file. -d FileName - FileName is a directory. -e FileName - FileName exists. -f FileName - FileName is a regular file. -g FileName - FileName's Set Group ID bit is set. -h FileName - FileName is a symbolic link. -k FileName - FileName's sticky bit is set. -L FileName - FileName is a symbolic link. -p FileName - FileName is a named pipe (FIFO). -r FileName - FileName is readable by the current process. -s FileName - FileName has a size greater than 0. -t FileDescriptor - FileDescriptor is open and associated with a terminal. -u FileName - FileName's Set User ID bit is set.

-w FileName - FileName's write flag is on. However, the FileName will not be writable on a read-only file system even if test indicates true.

-x FileName - FileName's execute flag is on. If the specified file exists and is a directory, the True exit value indicates that the current process has permission to change (chdir) into the directory.

file1 -nt file2 - file1 is newer than file2. file1 -ot file2 - file1 is older than file2. file1 -ef file2 - file1 is another name for file2. (symbolic link or hard link)

tring functions

-n String1 - the length of the String1 variable is nonzero. -z String1 - the length of the String1 variable is 0 (zero). String1 = String2 - String1 and String2 variables are identical. String1 != String2 - String1 and String2 variables are not identical. String1 - String1 variable is not a null string.

Number functions

Integer1 -eq Integer2 - Integer1 and Integer2 variables are algebraically equal. Any of the following comparisons can be used in place of -eq. -ne (not equal) -gt (greater than) -ge (greater or equal) -lt (less than) -le (less or equal)

Operators

These functions can be combined with the following operators: ! - Unary negation operator -a - Binary AND operator -o - Binary OR operator (the -a operator has higher precedence than the -o operator) (Expression) - Parentheses for grouping must be escaped with a backslash ().

The -a and -o operators, along with parentheses for grouping, are XSI extensions [ [http://www.opengroup.org/onlinepubs/009695399/utilities/test.html IEEE Std 1003.1, 2004, documentation for test] ] and are therefore not portable. In portable shell scripts, the same effect may be achieved by connecting multiple invocations of test together with the && and || operators and parentheses.

Exit Status

This command returns the following exit values:

0 - The Expression parameter is true. 1 - The Expression parameter is false or missing. >1 - An error occurred.

Examples

1. To test whether a file is nonexistent or empty, type:

if test ! -s "$1" then echo $1 does not exist or is empty. fi

If the file specified by the first positional parameter to the shell procedure, $1, does not exist, the test command displays an error message. If $1 exists and has a size greater than 0, the test command displays nothing.

Note: There must be a space between the -s function and the file name.

The quotation marks around $1 ensure that the test works properly even if the value of $1 is a null string. If the quotation marks are omitted and $1 is the empty string, the test command displays the error message

test: argument expected.

2. To do a complex comparison, type:

if [ $# -lt 2 -o ! -e "$1" ] then exit fi

If the shell procedure is given fewer than two positional parameters or the file specified by $1 does not exist, then the shell procedure exits. The special shell variable $# represents the number of positional parameters entered on the command line that starts this shell procedure.

External links

ee also

*List of Unix programs
*The bash command
*The csh command
*The find command
*The ksh command
*The sh command
*The zsh command


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Test (unix) — test est une commande Unix qui permet de créer des expressions conditionnelles. Sommaire 1 Syntaxe 2 Description 3 Fonctions 3.1 Fonctions de chaînes de caractères …   Wikipédia en Français

  • Test (UNIX) — test  файла и сравнения значений. Возвращает код возврата 0 (истина) или 1 (ложь) в зависимости вычисления выражения expr. Выражения могут быть как унарными, так и бинарными. Унарные выражения часто используются для проверки статуса файла. Также… …   Википедия

  • Test (Unix) — Pour les articles homonymes, voir Test. test est une commande Unix qui permet de créer des expressions conditionnelles. Sommaire 1 Syntaxe 2 Description …   Wikipédia en Français

  • test — test  UNIX утилита для проверки типа файла и сравнения значений. Возвращает код возврата 0 (истина) или 1 (ложь) в зависимости вычисления выражения expr. Выражения могут быть как унарными, так и бинарными. Унарные выражения часто… …   Википедия

  • Test — test  UNIX утилита для проверки типа файла и сравнения значений. Возвращает код возврата 0 (истина) или 1 (ложь) в зависимости вычисления выражения expr. Выражения могут быть как унарными, так и бинарными. Унарные выражения часто… …   Википедия

  • Test — Test, TEST or Tester may refer to:In science:* Experiment, part of the scientific method * Test (biology), the shell of sea urchins and certain microorganisms * Test method, a definitive procedure that produces a test result * Chemical test, a… …   Wikipedia

  • UNIX-Kommandos — Unix Systeme zeichnen sich durch eine Vielzahl von Kommandos aus, mit denen sich über eine Shell das Betriebssystem bedienen lässt. Die Syntax dieser Kommandos weicht unter den verschiedenen Systemen voneinander ab. Es existieren die beiden… …   Deutsch Wikipedia

  • Unix-Befehle — Unix Systeme zeichnen sich durch eine Vielzahl von Kommandos aus, mit denen sich über eine Shell das Betriebssystem bedienen lässt. Die Syntax dieser Kommandos weicht unter den verschiedenen Systemen voneinander ab. Es existieren die beiden… …   Deutsch Wikipedia

  • Unix-Kommando — Unix Systeme zeichnen sich durch eine Vielzahl von Kommandos aus, mit denen sich über eine Shell das Betriebssystem bedienen lässt. Die Syntax dieser Kommandos weicht unter den verschiedenen Systemen voneinander ab. Es existieren die beiden… …   Deutsch Wikipedia

  • Unix Kommandos — Unix Systeme zeichnen sich durch eine Vielzahl von Kommandos aus, mit denen sich über eine Shell das Betriebssystem bedienen lässt. Die Syntax dieser Kommandos weicht unter den verschiedenen Systemen voneinander ab. Es existieren die beiden… …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”