Test (Unix)

test is an order Unix which makes it possible to create conditional expressions.

Syntax

test Expression or Expression or Expression

Example: test - F /home/test or - F /home/test or - F /home/test

Description

the order test evaluates the expression placed in argument. In its second form, each hook () must obligatorily be separate condition by a space. The functions and operators are treated like different arguments by the order test. The conditional expression must refer to a condition of which it is possible to answer by true (true) or false (false).

Functions

The following functions are used to build the conditional expression and turn over all true (true) if the condition is satisfied:

- B File -- File exists and is a non-standard file in block mode. - C File -- File exists and is a non-standard file in character mode. - D File -- File exists and is a repertory. - E File -- File exists. - F File -- File exists and is of ordinary type. - G File -- File exists and the bit Set Group ID is active. - H File -- File exists and is a Lien symbolic system. - K File -- File exists and the sticky bit is active. - L File -- File exists and is a Lien symbolic system (even thing that - H ). - p File -- File exists and is a Tube named (named pipe, FIFO) . - R File -- File exists and is accessible in reading. - S File -- File exists and has a nonnull size. - U File -- File exists and the bit Set To use ID is active. - T Descriptor -- The descriptor of file is opened and associated with a terminal.

- W File -- File exists and is specified as being accessible in writing. However, the file will not be accessible in writing on a filing system in reading alone, even if the test indicates true (true).

- X File -- File exists and is specified as being achievable. If the specified file is a repertory, a value of true return (true) means that the process running to the permission to traverse this repertory.

Functions of character strings

- N Chaîne1 -- The length of Chaîne1 is not null. - Z Chaîne1 -- The length of Chaîne1 is null. Chaîne1 = Chaîne2 -- Chaîne1 and Chaîne2 are identical. Chaîne1 ! = Chaîne2 -- Chaîne1 and Chaîne2 are different. Chaîne1 -- Chaîne1 is not a null character string.

Numerical functions

Entier1 - eq Entier2 -- Entier1 and Entier2 are, algebraically speaking, equal. Entier1 - Entier2 -- Entier1 is not equal to Entier2. Entier1 - WP Entier2 -- Entier1 is strictly higher than Entier2. Entier1 - Ge Entier2 -- Entier1 is equal to or higher than Entier2. Entier1 - lt Entier2 -- Entier1 is strictly lower than Entier2. Entier1 it Entier2 -- Entier1 is lower or equal to Entier2.

Fichier1 - NT Fichier2 -- Fichier1 is more recent than Fichier2. Fichier1 - ot Fichier2 -- Fichier1 is older than Fichier2. Fichier1 - EFF Fichier2 -- Fichier1 and Fichier2 point towards the same file (Lien symbolic system).

Operators

All the functions above can be combined with the following operators: ! -- Unary operator of the negation - has -- Binary operator AND - O -- Binary operator OR (the operator - has is priority on the operator - O) \ (Expression \) -- The brackets to carry out groupings must be escaped by backslash (\).

Codes of return

The order test turns over the following values:

0 -- The conditional expression is true (true). 1 -- The conditional expression is false (false). >1 -- An error occurred.

Examples

1. To test if a file exists and is not empty:

yew test - S " $1" then echo " $1 exists and is not vide." fi

If the file specified in the first argument sent to script Shell ( $1 ) does not exist or is empty, the order test will turn over 1 and script nothing will post. But if on the contrary the file exists and is not empty, then the order test will turn over 0 and script will post " MonFichier exists and is not vide.".

Note: There must be a space between the option - S and the name of the file.

The quotation marks around $1 make it possible to make sure that the test will function even if the value of $1 is null. If the quotation marks are withdrawn and that $1 is not worth anything, then the order test will turn over an error of the " type; test: argument expected.".

2. To check that at least an argument was sent to Shell script:

yew $# - lt 1 then echo " Error: at least a argument." is needed; ; exit 2; fi

This time, the second notation of the order test (hooks) is used. In script above, $# corresponds to the number of arguments sent on the line of order. One looks at thus if this number is strictly lower ( - lt ) than 1 and if it is the case, one posts an error and one stops script while turning over an error code 2.

See too

Random links:Roller | List legislative districts Allier | Lista de restaurantes de cena ocasionales rápidos | Janette Khoury | Paper paper towel | Panthera spelaea | Trottoir_(architecture)