Design Scripts  «Prev  Next»
Lesson 5Checking commands
ObjectiveTest commands before using them in a script.

Determine Tests for Shell Script Loops

Why test commands?

You probably know dozens of UNIX commands, each with several options. But often a shell script requires the use of a command that you do not normally use on the command line. Before you enter these in a shell script, it’s a good idea to test them on the command line in a manner similar to how they will be used in your script.
Here is a simple example. Suppose you need to use the sort command in your script to organize a list of words. You’ve used the sort command previously, but in the script you will be sorting a file whose name is stored in a variable. To test this, you can use the variable on the command line and make certain that the sort command still works as expected:
$ FILENAME=wordlist.txt
$ sort $FILENAME

Testing commands before including them in a script helps in several ways:
  1. You see exactly how a command functions before, not after, a script is created and (perhaps) malfunctions.
  2. You can experiment more easily with command options that might be useful in the script.
  3. You can learn about non-standard commands (like dbconvert in our example outline) before they are used in your script.

All commands can be tested from a command line before including them in a script. As you learned earlier in this course, you can even test multiline if/then and looping commands from the command line. The shell waits until you “close” the command before executing the entire block of statements. (You learn about these types of commands in a future module.)
Adding code for foolproof scripts is the subject of the next lesson.

Which tests should a person writing shell scripts execute before running them in an environment

Before running a shell script in an environment, it's important to test the script thoroughly to ensure that it works as intended and doesn't cause any issues. Here are some tests that a person writing shell scripts can execute before running them in an environment:
  1. Syntax check: The first step is to check the syntax of the shell script to ensure that there are no syntax errors or other issues that could cause the script to fail. This can be done using the shell's built-in syntax checker or a separate syntax-checking tool, such as shellcheck.
  2. Dry run: Before running the script in a production environment, it's a good idea to do a dry run to see how the script will behave without actually making any changes. This can help identify any issues or unexpected behavior before the script is executed in a live environment.
  3. Test input/output: The script should be tested with a variety of input and output values to ensure that it behaves as expected in different scenarios. This can help identify any edge cases or issues with the script's handling of specific inputs or outputs.
  4. Permissions check: The script should be checked to ensure that it has the correct permissions to execute in the target environment. This can help prevent issues with file access, system resources, or other security-related issues.
  5. Code review: It's a good idea to have the script reviewed by a colleague or a peer to identify any issues, improve the code quality, and ensure that the script is consistent with the organization's coding standards.

By executing these tests before running a shell script in an environment, a person can help ensure that the script is robust, efficient, and safe to run, and that it will perform as expected without causing any issues or unexpected behavior.

Checking Commands - Exercise

Click the Exercise link to try what you have learned in the UNIX Lab.
Checking Commands - Exercise