Design Scripts  «Prev  Next»
Lesson 9

Designing More Complex Script Conclusion

This module provided guidance for designing a good shell script. You learned about planning a script in advance, diagramming what the script would do and testing each component of the script.

Guidelines to adhere when designing Shell Scripts for Unix

When designing shell scripts for Unix, there are several guidelines that you should adhere to in order to ensure that your scripts are well-organized, efficient, and easy to maintain. Here are some of the most important guidelines to keep in mind:
  1. Use clear and descriptive variable names: When defining variables in your shell script, use clear and descriptive names that make it easy to understand what the variable is used for.
  2. Indent your code properly: Proper indentation makes your code easier to read and understand. Use consistent and logical indentation to help other users understand the flow of your code.
  3. Add comments to your code: Adding comments to your code is a good practice that helps other users understand what your code does. Comments should explain why the code is needed, how it works, and any other relevant information.
  4. Use functions to organize your code: Functions can help you organize your code and make it easier to reuse. They can also make your code more modular and easier to test.
  5. Handle errors gracefully: When designing shell scripts, it is important to anticipate errors and handle them gracefully. This can include checking for the existence of files or directories, verifying that required environment variables are set, and checking for valid input.
  6. Use command-line arguments to customize behavior: Command-line arguments can be used to customize the behavior of your shell scripts. This can include setting variables, specifying input or output files, or enabling certain features.
  7. Test your scripts thoroughly: Thorough testing is important to ensure that your shell scripts work as expected. This can include testing for different input scenarios, edge cases, and error conditions.

By following these guidelines, you can design shell scripts that are well-organized, efficient, and easy to maintain, which can save you time and effort in the long run.

Key concepts

In this module you learned about these key concepts related to good script design:
  1. A well-formed script objective helps avoid wasted time as you start a shell script project.
  2. Outlining a script helps you define the best logic for the script and see where appropriate tests, loops, and error trapping can be used.
  3. Commands that you use within a script can be tested on the command line to save time debugging a script later on.
  4. Error trapping creates scripts that don’t crash or give unexpected results, but bullet-proof scripts require a lot of extra work to create.
  5. When possible, shell scripts should be written to run on a variety of UNIX operating systems by avoiding or testing for utilities and shell features that are unique to one system.
  6. Each script should be documented so that users can determine how to use it. This can be done in script comments, in usage messages, or in standard online or written documentation.

The next module is all about reading data from files and writing data to files using common UNIX commands and syntax within shell scripts.