Previous | Table of Contents | Next |
Common Errors
Three common errors can occur when using the if statement:
As an example of the first type of error, if you leave out the ; from the previous example
if uuencode koala.gif koala.gif > koala.uu then echo "Encoded koala.gif to koala.uu" else echo "Error encoding koala.gif" fi
an error message appears:
ch11-ex1.sh[2]: Syntax error at line 5 : 'else' is not expected.
If you see this type of error, make sure that a semicolon precedes the then statement.
The second type of error can be illustrated using the following if statement:
if uuencode koala.gif koala.gif > koala.uu ; then echo "Encoded koala.gif to koala.uu" elif rm koala.uu ; then echo "Encoding failed, temporary files removed." else echo "An error occured." fi
Here you have an elif statement that removes the intermediate file koala.uu, if the uuencode fails. If the elif is changed to an else if as follows
if uuencode koala.gif koala.gif > koala.uu ; then echo "Encoded koala.gif to koala.uu" else if rm koala.uu ; then echo "Encoding failed, temporary files removed." else echo "An error occured." fi
an error message similar to the following is generated:
./ch11-ex1.sh: ./ch11-ex1.sh: line 8: syntax error: unexpected end of file
If the elif statement is changed to elsif as follows
if uuencode koala.gif koala.gif > koala.uu ; then echo "Encoded koala.gif to koala.uu" elsif rm koala.uu ; then echo "Encoding failed, temporary files removed." else echo "An error occured." fi
an error message similar to the following is generated:
./ch11-1.sh: syntax error at line 4: 'then' unexpected
If the then statement was omitted after the elif statement as follows
if uuencode koala.gif koala.gif > koala.uu ; then echo "Encoded koala.gif to koala.uu" elif rm koala.uu echo "Encoding failed, temporary files removed." else echo "An error occured." fi
an error message similar to the following is generated:
./ch11-1.sh: syntax error at line 6: 'else' unexpected
Finally, if the fi statement is written as if, an error message such as the following is generated:
./ch11-1.sh: syntax error at line 8: 'end of file' unexpected
This indicates that the if statement was not closed with a fi statement before the end of the script.
Most often, the list given to an if statement is one or more test commands, which are invoked by calling the test command as follows:
test expression
Here expression is constructed using one of the special options to the test command. The test command returns either a 0 (true) or a 1 (false) after evaluating an expression.
A shorthand for the test command is the [ command:
[ expression ]
Here expression is any valid expression that the test command understands. This shorthand form is the most common form of test that you can encounter.
The types of expressions understood by test can be broken into three types:
You look at each of these types in turn. You also look at compound expressions, formed by combining two or more test expressions.
Note:
When using the [ shorthand for test, the space after the open bracket ([) and the space before the close bracket (]) are required.Without these spaces, the shell cannot tell where the expression begins and ends.
Previous | Table of Contents | Next |