DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Automating frequent tasks

Different kinds of test

In general, tests are carried out either by enclosing them in square braces (as above) or by using the command test(C). The most useful tests are as follows:


-r file
True if a file called file exists and is readable.

-w file
True if a file called file exists and is writable.

-x file
True if a file called file exists and is executable.

-s file
True is a file called file exists and is not empty.

-d file
True if a file called file exists and is a directory.

-f file
True if a file called file exists and is a regular file.

-z string
True if the length of string is zero.

-n string
True if the length of string is non-zero.

string1 = string2
True if string1 equals string2.

string1 != string2
True if string1 is not equal to string2.

number1 -eq number2
True if the integer number1 equals number2.

number1 -ne number2
True if the integer number1 is not equal to number2.

number1 -gt number2
True if the integer number1 is greater than number2.

number1 -lt number2
True if the integer number1 is less than number2.

number1 -ge number2
True if the integer number1 is greater than or equal to number2.

number1 -le number2
True if the integer number1 is less than or equal to number2.

In addition to these tests, there are a number of others; see test(C) for details. In general, the tests listed here should be sufficient to let you test for the existence of files, to check whether your script has permission to manipulate a given file, to compare two numbers, and to see if a string matches some value. These are the commonest comparisons used to help a script decide on a course of action to take.
Next topic: Testing exit values
Previous topic: Choosing one of two options: the if statement

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003