The Linux Command Line Chapter 26 Top Down Design Prepared by Dr. Reyes, New York City College of Technology
Top Down Design As programs get larger and more complex, they become more difficult to design, code and maintain. It is often a good idea to break large, complex tasks into a series of small, simple tasks.
Top Down Design Instructions to buy food Get in car. Drive to market. Park car. Enter market. Purchase food. Return to car. Drive home. Enter house. Subtasks for step 3, 8 “Park Car” 1. Find parking space. 2. Drive car into space. 3. Turn off motor. 4. Set parking brake. 5. Exit car. 6. Lock car.
Simpler form (preferred) Shell Functions Shell functions - “mini-scripts” that are located inside other scripts and can act as autonomous programs. Formal form Simpler form (preferred)
Sample Script with Functions
Global vs Local Variables Global variables – variables that maintain their existence throughout the program and can be accessed anytime by any function. Local variables – variables defined inside shell functions and are only accessible within the shell function in which they are defined and cease to exist once the shell function terminates. Use keyword local in front of the variable After that, in a separate line assign a value to the variable
Example Using Local Variables