Note: The following material was developed by Claude Cantin of the Research Computing Support Group of the National Research Council of Canada, and is used with his permission.

Subroutines

Scripts, like any other programming language, may contain subroutines. A common way to use a subroutine is:

    #!/bin/sh

    usage()
    {
        echo " "
        echo "You have not used this program correctly."
        echo "Use as:  prog_name par1 par2 ..."
        echo " "
        exit 1
    }

    one_par()
    {
        echo
        echo There was only one parameter on the command line.
        echo
        return 1
    }

    two_par()
    {
        echo
        echo There were two parameters on the command line.
        echo
        return 1
    }

    larger()
    {
        echo
        echo There were many parameters.
        echo
        return 1
    }

    case $# in
        0) usage;;
        1) one_par ;; 
        2) two_par ;; 
        *) larger ;;
    esac
    exit 0
Note that routines must be defined prior to being used and that all variables are global.