The test
builtin command returns 0 (True) or 1 (False), depending on the evaluation of an expression, expr. You can also use square brackets: test expr
and [ expr ] are equivalent. You can examine the return value by displaying $?
; you can use the return value with && and ||; or you can test it using the various conditional constructs that are covered later in this tip.
Listing 1. Some simple tests
|
In the first example in Listing 1, the -gt
operator performs an arithmetic comparison between two literal values. In the second example, the alternate [ ]
form compares two strings for inequality. In the final example, the value of the HOME variable is tested to see if it is a directory using the -d
unary operator.
You can compare arithmetic values using one of -eq
, -ne
, -lt
, -le
, -gt
, or -ge
, meaning equal, not equal, less than, less than or equal, greater than, and greater than or equal, respectively.
You can compare strings for equality, inequality, or whether the first string sorts before or after the second one using the operators =
, !=
, <
, and >
, respectively. The unary operator -z
tests for a null string, while -n
or no operator at all returns True if a string is not empty.
Note: the <
and >
operators are also used by the shell for redirection, so you must escape them using \<
or \>
. Listing 2 shows more examples of string tests. Check that they are as you expect.
Listing 2. Some string tests
|
Some of the more common file tests are shown in Table 1. The result is True if the file tested is a file that exists and that has the specified characteristic.
Operator | Characteristic |
---|---|
-d | Directory |
-e | Exists (also -a) |
-f | Regular file |
-h | Symbolic link (also -L) |
-p | Named pipe |
-r | Readable by you |
-s | Not empty |
-S | Socket |
-w | Writable by you |
-N | Has been modified since last being read |
In addition to the unary tests above, you can compare two files with the binary operators shown in Table 2.
Operator | True if |
---|---|
-nt | Test if file1 is newer than file 2. The modification date is used for this and the next comparison. |
-ot | Test if file1 is older than file 2. |
-ef | Test if file1 is a hard link to file2. |
No comments:
Post a Comment