Unix Commands  «Prev  Next»
Lesson 2Types of errors
ObjectiveIdentify syntax errors and logic errors.

Types of errors

Errors in shell scripts can be divided into many categories. Two of the most fundamental are syntax errors and logic errors.

Syntax errors


Syntax errors are misspellings or missing pieces of code in a script. These are usually the easiest errors to spot because they generate an error message when you try to run the code. The shell will try to identify which line of your script is causing the error.
The following code has a syntax error. There should be no space before the equal sign.
num1 = 34

An error on one line of code will sometimes not show up until later in your script. Therefore, the shell sometimes identifies the wrong line of your code as containing the syntax error When you receive an error message that specifies a line number, you may have to go to each line before that line number to find the actual error.

Logic errors

Logic errors often are the most difficult to locate. When your code tests for the wrong value, you have a logic error. For example, if your program is supposed to print out the last five lines in a file and it instead prints out the last four, you have a logic error. With this type of error, your code will run with no syntax errors identified, making the error hard to spot. You may have to examine your code line by line to find the error.
The following code checks for the value “mo” instead of the value “no”.
if["$answer" = "mo" ] 
then echo "I will exit the program now" 
exit 
fi

The next lesson explores options to the shell that help identify the errors in a script.