Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Useful Functions

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

  printERROR
  printWARNING
  printUSAGE
  promptYESNO
  promptRESPONSE
  getSpaceFree
  getSpaceUsed
  getPID
  getUID

In addition, you will be asked to develop four additional functions as part of the “Questions” section in this chapter:

  toUpper
  toLower
  isSpaceAvailable
  isUserRoot

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 function’s 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.

Displaying Messages

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 message prefix, either ERROR, WARNING, or USAGE.
  The arguments are specified to these functions by the user. The arguments are stored in $@, as explained in Chapter 12, “Parameters.”

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 don’t 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