#!/bin/bash
#
# compute n factorial using recursive function (version from 9/07 class)
# (an example of recursion)
#
# checks argument for integer
#
function factorial() {
if [ $1 -eq 0 ]
then
    echo 1
else
    t=`factorial \`expr $1 - 1\``
    expr $1 \* $t
fi
}
if [ -z "$1" ]
then
    echo usage `basename $0` n
    exit 1
fi

tmp=`expr "$1" + 0 2>/dev/null`
if [ "$?" -ne 0 ]
then
    echo "$1" not an integer
    exit 1
fi
if [ "$1" -lt 0 ]
then
    echo "$1" is negative
    exit 1
fi
echo factorial of $1 is:
factorial $1
