next up previous contents
Next: Miscellaneous Up: Programming the Shell Previous: expr: Doing Arithmetic

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.



Claude Cantin
Sun Sep 1 02:02:26 EDT 2002