Branching decisions are made depending on the result of a test command. The test command can perform tests on numeric and string data as well as on files. The test command returns a true or false value. The true value is always zero, while false is a number other than zero. Usually this number is one. You can check the result code of the test command to make a branching decision. The test command can be used in explicit or implicit modes. In the explicit mode, the test command is used as follows .
$ test "ABC" = "abc"
In the implicit mode, the word " test " is not there; square brackets are used instead.
$ [ "ABC" = "abc" ]
The command does not print anything on the terminal screen. The result code can be checked using the $? variable as explained earlier in the chapter.
Tests can be performed to compare two or more integers. The relations that can be used with numeric data are shown in Table 10-3.
Relation | Description |
---|---|
-eq | Equality check |
-ne | Not equal |
-lt | Less than |
-gt | Greater than |
-le | Less than or equal to |
-ge | Greater than or equal to |
Numeric testing will be used in shell programs later in this chapter.
The string values can be checked for equality and nonequality. Other than that, a single string can be tested if it has a zero length or not. The string operations are shown in Table 10-4.
Operation | Description |
---|---|
string1 = string2 | True if string1 and string2 are equal |
string1 != string2 | True if string1 is not equal to string2 |
-z string | True if string length is zero |
-n string | True if string length is nonzero |
string | True if string length is nonzero |
Testing on files can be performed in many ways. Some of these are shown in Table 10-5. A list of other supported file tests can be found using the man sh-posix command.
Operation | Description |
---|---|
-d file | True if the file is a directory |
-f file | True if the file exists and is a normal file (not a directory) |
-s file | True if the file is more than zero bytes in length |
-r file | True if the file is readable |
-w file | True if the file is writable |
-e file | True if the file exists |
-L file | True if the file is a symbolic link |
file1 -nt file2 | True if file1 is newer than file2 |
file1 -ot file2 | True if file1 is older than file2 |
-x file | True if the file is executable |
For example, if file file1 exists, and you use the following command to check its existence, you will get an exit code of zero (true).
$ [ -f file1 ] $ $ echo $? 0 $
Logical operations can be performed on two expressions with one of the logical operators shown in Table 10-6.
Operation | Description |
---|---|
expr1 -o expr2 | Logical OR, true if either expr1 or expr2 is true |
expr1 -a expr2 | Logical AND, true if both expr1 and expr2 are true |
! expr | Logical NOT, true if expr is false |
The following code segment tests files represented by the first two command line arguments and prints a message if both files exist.
#!/usr/bin/sh if [ -f -a -f ] then echo "Test successful" fi
Top |