|Lesson 7|| Creating a reusable function|
|Objective||Write a reusable function.|
Creating reusable Functions for Shell Script
Once you begin writing your own shell scripts, you can recycle your code in other programs. If you write your functions in a generic manner, you can reuse your already-written functions in new scripts. If you write your functions to contain the following properties, they may be good candidates for reuse. A reusable function:
- Should perform a single task
- Does not contain hard-coded filenames
- Has a descriptive name
- Gets its information through arguments when it is called, not through variables
- Has function usage and purpose included in comments inside the function
How to make Shell Scripts Portable
Making shell scripts portable means ensuring that the scripts will work correctly across different Unix-based systems, such as Linux, macOS, and various Unix distributions. Here are some considerations to keep in mind when writing portable shell scripts:
- Choose a common shell: Use a shell that is common across different Unix-based systems. The most common shell used in Unix-based systems is the Bourne-Again SHell (Bash). Avoid using shell-specific features that might not be available in other shells, such as the C shell (csh) or the Z shell (zsh).
- Use absolute paths: Use absolute paths to reference files, directories, and executables, as opposed to relative paths. Absolute paths do not depend on the current working directory, which can vary across systems.
- Be mindful of command options: Avoid using command options or switches that are specific to a particular version of a command or utility. Instead, use options that are common and available across different versions of the command.
- Avoid shell-specific syntax: Avoid using shell-specific syntax or features, as different shells may handle these features differently or not at all. Use syntax that is common and available across different shells.
- Check for command availability: Check for the availability of commands, utilities, and tools before using them. Use alternative commands or utilities if a particular command or utility is not available or does not work as expected.
- Use portable code constructs: Use code constructs that are known to be portable and widely supported across different Unix-based systems, such as POSIX (Portable Operating System Interface) shell constructs.
- Test on different systems: Test the script on different Unix-based systems to ensure that it works correctly and produces the expected output.
# this function takes three arguments,
# argument 1 is the field separator for the file
# argument 2 is the number of the column to sort on
# argument 3 is the name of the file to be sorted
# this information is used to sort the file on the given column
if [ $# -eq 2 ]
sort –t$1 +$2 $3
sortfunc () # The function has a descriptive name.
$3, The function does not use hard-coded file names.
$1, The function uses arguments for information, not variables.
#, This function takes three arguments. The function contains comments on how to use it and its purpose.
The next lesson looks at precautions for using scripts written by others.