Previous | Table of Contents | Next |
Notice that the variable $FRUIT_BASKET is quoted in this example. This is required in the event that the variable is unset. If $FRUIT_BASKET is not quoted, an error message is displayed when it is unset:
test: argument expected
This error message is presented because the shell does not quote the null value of $FRUIT_BASKET. The resulting test looks like
[ -z ]
Because the string argument is missing, test complains that a required argument is missing. By quoting $FRUIT_BASKET, the test looks like
[ -z "" ]
Here the required string argument is "".
Checking Whether Two Strings Are Equal The test command enables you to determine whether two strings are equal. Two strings are considered equal if they contain exactly the same sequence of characters. For example, the strings
"There are more things in heaven and earth" "There are more things in heaven and earth"
are equal, but the strings
"than are dreamt of in your philosophy" "Than are dreamt of in your Philosophy"
are not equal because of the differences in capitalization.
The basic syntax for checking whether two strings are equal is
test string1 = string2
or
[ string1 = string2 ]
Here string1 and string2 are the two strings being compared.
A simple example of using string comparisons is the following:
if [ "$FRUIT" = apple ] ; then echo "An apple a day keeps the doctor away." else echo "You must like doctors, your fruit $FRUIT is not an apple." fi
If the operator != is used instead of =, test returns true if the two strings are not equal. Using the != operator, you can rewrite the previous command as follows:
if [ "$FRUIT" != apple ] ; then echo "You must like doctors, your fruit $FRUIT is not an apple." else echo "An apple a day keeps the doctor away." fi
Numerical Comparisons
The test command enables you to compare two integers. The basic syntax is
test int1 operator int2
or
[ int1 operator int2 ]
Here int1 and int2 can be any positive or negative integers and operator is one of the operators given in Table 10.3. If either int1 or int2 is a string, not an integer, it is treated as 0.
Operator | Description |
---|---|
int1 -eq int2 | True if int1 equals int2. |
int1 -ne int2 | True if int1 is not equal to int2. |
int1 -lt int2 | True if int1 is less than int2. |
int1 -le int2 | True if int1 is less than or equal to int2. |
int1 -gt int2 | True if int1 is greater than int2. |
int1 -ge int2 | True if int1 is greater than or equal to int2. |
Among the most common tasks in a shell script are executing a program and checking its return status. By using the numerical comparison operators, you can check the return or exit status of a command and perform different actions when a command is successful and when a command is unsuccessful.
For example, consider the following command:
ln -s /home/ranga/bin/bash /usr/contrib/bin
If you execute this command on the command line, you can see any error messages and intervene to fix the problem. In a shell script, the error message is ignored and the script continues to execute. For this reason, it is necessary to check whether a program exited successfully.
As you saw with the test command, an exit status of 0 is successful, whereas nonzero values indicate some type of failure. The exit status of the last command is stored in the variable $?, so you can check whether a command was successful as follows:
if [ $? -eq 0 ] ; then echo "Command was successful." ; else echo "An error was encountered." exit fi
If the command exits with an exit code of 0, issue the good message; otherwise, issue an error message and then exit. This can be simplified as follows:
if [ $? -ne 0 ] ; then echo "An error was encountered." exit fi echo "Command was successful."
Here you check to see whether the command failed. If so, you echo an error message and exit: otherwise, the if statement completes and the good message is issued. This is slightly more efficient than using an else clause.
Previous | Table of Contents | Next |