Previous | Table of Contents | Next |
Now that I have covered the background knowledge needed to create and use a library of shell functions, look at a shell library that provides you with the capability to perform common scripting tasks easily. A complete listing of the library is available at the end of this chapter.
Some of the functions you will look at are
In addition, you will be asked to develop four additional functions as part of the Questions section in this chapter:
By developing these functions, you can gain experience in working with a library of shell functions.
In the following sections, I will first present a brief description of each function or group of functions, followed by the implementation of the functions. At the end of each section is a discussion of the functions implementation along with caveats regarding its use.
Some of these functions need to be modified to work properly on all versions of UNIX. In this chapter, I will note the differences. In Chapter 23, Scripting for Portability, I will show you how to modify these functions to account for the differences between different versions of UNIX.
Most of the messages that shell scripts display do not need special handling, such as prefixing the message with a description or having the output redirected to STDERR. The exceptions are error, warning, and usage messages. These messages require some extra handling. The following functions take care of this handling for you:
################################################ # Name: printERROR # Desc: prints an message to STDERR # Args: $@ -> message to print ################################################ printERROR() { echo "ERROR:" $@ >&2 } ################################################ # Name: printWARNING # Desc: prints an message to STDERR # Args: $@ -> message to print ################################################ printWARNING() { echo "WARNING:" $@ >&2 } ################################################ # Name: printUSAGE # Desc: prints a USAGE message and then exits # Args: $@ -> message to print ################################################ printUSAGE() { echo "USAGE:" $@ exit }
All these commands work by calling the echo command and passing it two arguments:
The first two functions, printERROR and printWARNING, display error and warning messages. Both messages indicate to a user that something has gone wrong. Thus they redirect the output to STDERR, which is reserved for error reporting.
Usually an error indicates the occurrence of something unexpected that is difficult to recover from, such as a command failure. A warning message usually indicates that something unexpected occurred, but the script was able to recover from this.
The advantage of using these functions is that they provide a standard output format for errors and warning throughout you script. As an example, the following error message
echo "ERROR: File $MYFILE was not found." >&2
can be written
printERROR "File $MYFILE was not found."
Because the function printERROR always prefixes the message you want to display with the word ERROR and redirects the output to STDERR, you dont have to worry about forgetting these things when displaying an error message.
The third function, printUSAGE, displays a usage message and then exits. It informs the user that the script was invoked incorrectly. This type of message was discussed in depth in Chapter 12.
Previous | Table of Contents | Next |