Shell Functions  «Prev  Next»
Lesson 9

Writing Shell Functions Conclusion

This module introduced you to defining and using functions. You learned how to determine a good name for a function, properties of reusable functions, and how to pass information to functions through function arguments. Using functions created by others can be dangerous, so precautions should be taken when doing this. Before attempting to write a shell script, a developer should contemplate the following:
  1. Purpose: The developer should have a clear understanding of the purpose and scope of the script, including what tasks it should perform, what inputs it will need, and what outputs it should generate. This will help to ensure that the script meets the desired goals and requirements.
  2. Shell choice: The developer should choose an appropriate shell for the script based on the operating system, system requirements, and compatibility with other scripts and tools.
  3. Script structure: The developer should plan the structure and flow of the script, including the use of functions, variables, and control structures like loops and conditional statements.
  4. Script inputs: The developer should identify the inputs required by the script, such as user inputs or input files, and consider how these inputs will be validated and processed.
  5. Script outputs: The developer should determine the desired output of the script and how it will be generated and presented, such as printing to the console or writing to a file.
  6. Error handling: The developer should consider how errors and unexpected inputs will be handled, including how the script will detect errors, generate error messages, and exit gracefully.
  7. Security: The developer should consider the security implications of the script, including how it will handle sensitive data or access sensitive system resources.
  8. Documentation: The developer should plan to include documentation and comments within the script, describing the purpose, inputs, and outputs of the script, as well as any other important information.

Considering these factors before attempting to write a shell script can help the developer to create a well-structured, functional, and secure script that meets the desired requirements and can be easily maintained and reused.

Glossary terms

This module introduced you to the following terms:
  1. Function
  2. Function call
  3. Function definition
  4. Function Argument

Function Argument in a Unix Shell Script?

In Unix Shell Scripting, a function argument is a value or variable that is passed to a function when it is called. The arguments are passed to the function as parameters, which are enclosed in parentheses after the function name.
The syntax for defining a function with arguments is as follows:
function_name () {
  # Function code goes here
  # $1 is the first argument, $2 is the second argument, etc.

In this syntax, function_name is the name of the function, and $1, $2, etc. represent the first argument, second argument, etc.
When the function is called, the arguments are passed in the same order as they are defined in the function definition. For example, to call the function_name function with two arguments, you would use the following syntax:
function_name arg1 arg2

In this example, arg1 would be passed as the first argument (and would be accessible within the function as $1), and arg2 would be passed as the second argument (and would be accessible within the function as $2).
Using arguments in functions can make your scripts more flexible and reusable, as you can define the function to perform a specific task with a set of variables or values that can be changed when the function is called. It also helps to reduce code duplication by allowing you to define a function once and call it multiple times with different arguments.

What are the properties of reusable Script Functions in Unix?

Reusable script functions are an important component of Unix shell scripting. They have several properties that make them useful and powerful:
  1. Reusability: Functions are reusable code blocks that can be called from multiple places in a script or even from other scripts. This makes them useful for code that is used frequently and helps to avoid duplication of code.
  2. Modularity: Functions can be written as standalone units of code that perform a specific task. This makes them easy to understand and maintain, as you can focus on a specific task and its implementation, without worrying about the rest of the code.
  3. Abstraction: Functions can be used to abstract complex or repetitive tasks, making the code more concise and easier to read. This can be especially useful when the function is used to perform a task that involves multiple commands or complex logic.
  4. Encapsulation: Functions can encapsulate complex or critical operations, reducing the risk of error or damage to the system. This can help to ensure that critical tasks are performed consistently and without error.
  5. Parameterization: Functions can be defined with parameters, which allows them to be customized for different use cases. This can help to make the code more flexible and adaptable, without having to write multiple versions of the function.
  6. Portability: Functions can be written in a way that makes them portable across different Unix systems, making them useful for code that needs to be used on multiple systems.

By leveraging these properties, reusable script functions can help to make Unix shell scripts more efficient, maintainable, and flexible.
In the next module, you will learn to use the UNIX sed command to edit a file in a shell script.

Print Menu Function - Exercise

Click the exercise link to practice adding functions to the course project script.
Print Menu Function - Exercise