Module 135 test

Previous Table of Contents Next


Module 135
test

DESCRIPTION

The internal test command is used to evaluate conditional expressions. The test command evaluates the expression and, if it is true, returns a 0 return code; if the expression is false, the return code is 1. The test command is primarily used to make a conditional decision in a shell script. The following features are provided:

   Return code used by shell constructs; if, until, and while.
   Evaluates conditional (true or false) expressions.
   Returns an exit status equal to 0 if condition is true.
   Returns an exit status greater than 0 if condition is false.
   Evaluates file permissions and types.
   Evaluates numerical expressions.
   Evaluates string expressions.

There are internal versions of test in the csh and ksh and an external version in /bin/test. This module explains the ksh version and the common BSD /bin/test version.

COMMAND FORMAT

Following is the general format of the test command.

 test expr      [ expr ] 

The brackets ([]) are an alternative to the test syntax. You must place blanks before and after the first bracket and before the second one. Thus the following two syntaxes are equivalent:

 if test -f file      if [ -f file ] 

EXPRESSIONS

The test command has primary expressions (operators) that perform one specific conditional check. These primary expressions can be combined using operators. The following three sections cover the primary expressions used to test files, strings, and numbers .

Testing on files

These primitives can be used to test the state, type, or permissions of a file. The file may be a constant or variable.


NOTE:    
If a file does not exist or the condition is false, test returns an exist status of nonzero. If the condition is true, test returns an exit status of 0.


Expression Description

-b file True if file exists and is a block special file.
-c file True if file exists and is a character special file.
-d file True if file exists and is a directory.
-f file True if file exists and is a regular file.
-g file True if file exists and has the set- group -ID bit set.
-k file True if file exists and has the sticky bit set.
-p file True if file exists and is a pipe.
-r file @LIST2= True if file exists and is readable.
-s file True if file exists and has a size greater than 0 bytes.
-t [fds] True if the open file defined by file descriptor fds is a tty(default fds=1).
-u file True if file exists and has the set- user -ID bit set.
-w file True if file exists and is writable.
-x file True if file exists and is executable.

Korn Shell Extensions
-L file True if file exists and is a symbolic link.
file1 -nt file2 True if file1 is newer than file2 .
file1 -ot file2 True if file1 is older than file2 .
file1 -ef file2 True if file1 has the same device and inode number as file2 .

If file is of the form /dev/fd/ n , where n is an integer, the test is applied to the open file whose descriptor number is n .

BSD (Berkeley)
Only the following file testing options exist in BSD versions.
-L file True if file exists and is a soft link.
-d file True if file exists and is a directory.
-f file True if file exists and is a regular file.
-r file True if file exists and is readable.
-s file True if file exists and has a size greater than 0 bytes.
-t [fds] True if the open file defined by file descriptor fds is a tty(default fds=1).
-w file True if file exists and is writable.


NOTE:    
It is advisable to enclose string variables within double quotes. This ensures that test will work even if the variable is unset or null.

Testing on strings

The following primitives can be used to test the condition of strings. The sX values may be a constant or a variable. The double quotes around the sX values are not necessary but are advisable.


Expression Description

-z s1 True if length of string s1 is zero.
s1 = s2 True if strings s1 and s2 are equal(match).
s1 != s2 True if strings s1 and s2 are NOT equal.
s1 True if s1 exists; is not a null string.
The = and != operators have higher precedence than the -b through -z operators.

BSD (Berkeley)
BSD supports all of these options.

Testing on numbers

The following primitives can be used to test the condition of two numbers. The double quotes around the sX values are not necessary but are advisable.


Expression Description

n1 -eq n2 True if n1 and n2 are equal.
n1 -ne n2 True if n1 and n2 are NOT equal.
n1 -gt n2 True if n1 is greater than n2 .
n1 -ge n2 True if n1 is greater than or equal to n2 .
n1 -lt n2 True if n1 is less than n2 .
n1 -le n2 True if n1 is less than or equal to n2 .

BSD (Berkeley)
BSD supports all of these options.


Previous Table of Contents Next

Copyright Wordware Publishing, Inc.


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net