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

Types of Unit Scripting Errors

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

Identify syntax errors and logic errors in Unix Shell Script
Syntax errors are errors that occur due to incorrect syntax or formatting in the script. These errors can be identified by the shell when the script is run, and they will usually cause the script to fail to execute. Some examples of syntax errors include:
  1. Incorrect spelling of commands or variables
  2. Missing or incorrect quotation marks
  3. Using incorrect operators or parentheses
  4. Missing or misplaced semicolons or colons

On the other hand, logic errors occur when the script runs but produces unexpected or incorrect results. These errors can be more difficult to identify since the script will run without throwing an error, but the output will not be what is expected.
Some examples of logic errors include:
  1. On the other hand, logic errors occur when the script runs but produces unexpected or incorrect results. These errors can be more difficult to identify since the script will run without throwing an error, but the output will not be what is expected. Some examples of logic errors include:
  2. Incorrect use of conditional statements or loops
  3. Incorrect use of variables or command output
  4. Incorrect file or directory paths
  5. Incomplete or incorrect logic flow

To identify these errors, it is often helpful to run the script with debugging enabled or to add logging statements throughout the script. This will help you see where the script is going wrong and identify the specific lines of code that need to be fixed. Syntax errors are identified by the shell during script execution due to incorrect syntax or formatting, while logic errors occur when the script runs but produces unexpected or incorrect results. To identify these errors, use debugging and logging statements throughout the script.

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.