Shell Functions

From The System Administrator Zone


One of the handiest ways to keep your shell scripts consistent is to use a separate file to hold standard functions. You can include that file in your scripts, using the source command, and use the functions as part of your process.

See Functions.inc for one example.

General Information About Using Shell Functions

No /bin/sh at the Start

Do not include an #!/bin/sh at the start of a separate function file as you are not looking to spawn another shell.

Returning a Value from a Function

Normally returns its exist status. However, you can have it return any value desired. Say, for example, you wanted it to return the "count"...

count=8
return $count

Sourcing the Function File

Assuming your function file is called function.inc, you would incorporate it into your script with a single source statement:

source functions.inc

I normally set a variable with the location of the script files and include that as part of the command.

BIN=/path/to/script/file/bin
source ${BIN}/function.inc

Sample Functions

Echo to Standard Error

This will echo to file descriptor 2, standard error.

# Echo to file descriptor 2 (stderr).
e2 () { echo "$@" >&2; }

Usage: the quotes are optional, but used for consistency.

e2 "Text to be printed goes here..."

Testing the Usability of a Directory

To test that a directory exists and is usable:

# Returns true (0) only if it is a directory and searchable.
test_directory () {
        test "$#" -eq 0 && e2 "Usage: test_directory dirname" && return 2
        test -d "$1" && test -x "$1"
}

Usage: This example will use the e2 function to print an error to stderr and then exit the script with a status of 1 if the function returns anything other than zero.

test_directory "${TMP}" || ( e2 "$TMP is not a usable directory" && exit 3 )
test "$?" -ne 0 && exit 1

The first exit returns the value 3 from the script in the brackets (), the second exit kills the top level script.

Testing the Usability of a File

To test that a file exists and is usable:

# Returns true (0) only if it is a file and is can be executed.
test_exec () {
        test "$#" -eq 0 && e2 "Usage: test_file filename" && return 2
        test -f "$1" && test -x "$1"
}

Usage: This example will use the e2 function to print an error to stderr and then exit the script with a status of 1 if the function returns anything other than zero.

test_exec "${TMP}" || ( e2 "$TMP is not a usable file" && exit 3 )
test "$?" -ne 0 && exit 1

The first exit returns the value 3 from the script in the brackets (), the second exit kills the top level script.

Testing the Readability of a File

To test that a file exists and is usable:

# Returns true (0) only if it is a file and is can be executed.
test_read () {
        test "$#" -eq 0 && e2 "Usage: test_file filename" && return 2
        test -f "$1" && test -r "$1"
}

Usage: This example will use the e2 function to print an error to stderr and then exit the script with a status of 1 if the function returns anything other than zero.

test_read "${TMP}" || ( e2 "$TMP is not a readable file" && exit 3 )
test "$?" -ne 0 && exit 1

The first exit returns the value 3 from the script in the brackets (), the second exit kills the top level script.