#!/bin/bash

# adapted from http://steve-parker.org/sh/functions.shtml
#
# example of a recursive function -- the old classic, factorial.
#
# adapted by: Sharon Tuttle
# last modified: 11-19-12

# expects an integer as its single command-line argument,
#    outputs the factorial of that integer to standard output

factorial()
{
    # recursive case

    if [ "$1" -gt "1" ]
    then
        one_less=`expr $1 - 1`
        fact_one_less=`factorial $one_less`
        desired_fact=`expr $1 \* $fact_one_less`
        echo $desired_fact

    # base case

    else
        echo 1
    fi
}

# call factorial function with an argument requested from the user

echo "Enter a number:"
read num
factorial $num