Before you start writing a complex shell script, you should plan what the script will do and how it will do it.
This planning is a critical part of designing an effective script in any programming language. Without thinking through your script before you start writing it, you are likely to waste a lot of time and have an inefficient script when you have finished.
By the end of this module you will be able to:
- Define a clear script objective
- Diagram the flow of control through a script
- Decide which types of tests will work best in your script
- Test commands before using them in a script
- Include error-checking in your script
- Create a script that works in multiple shells
Defining a clear objective is an important first step when creating a Unix shell script. Here are some steps you can follow to help you define your objective:
- Identify the problem: The first step is to identify the problem you want to solve with your shell script. What is the task that you want your script to accomplish? This could be anything from automating a repetitive task, to processing large amounts of data, to creating backups of important files.
- Determine the scope: Once you have identified the problem, it is important to determine the scope of the project. What are the specific requirements for the script? What are the limitations? What are the dependencies?
- Define the output: What is the expected output of the script? Do you want the script to generate a report, a file, or just display information on the screen?
- Identify the inputs: What inputs will the script need to accomplish its objective? This could be data from files, user input, or data from other programs.
- Break down the steps: Once you have identified the inputs and outputs, it is important to break down the task into smaller steps. What are the specific tasks that the script needs to perform in order to accomplish its objective?
- Define success: Finally, it is important to define what success looks like for your script. How will you know that your script has accomplished its objective?
By following these steps, you can define a clear objective for your Unix shell script, which will make it easier to develop and test your script, and ensure that it is successful in achieving its intended purpose.