#!/usr/bin/sh # Copyright 2005 Daniel Cer (daniel.cer@cs.colorado.edu) # # This work is licensed under the Creative Commons Attribution-NonCommercial- # ShareAlike License. To view a copy of this license, visit # http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to # Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, # 94105, USA. function fibonacci { if [ $1 -le 0 ]; then fibonacci_ret=-1; return; fi if [ $1 -le 2 ]; then fibonacci_ret=1; return; fi fibonacci $[$1-1] local fib_n_sub1=$fibonacci_ret fibonacci $[$1-2] local fib_n_sub2=$fibonacci_ret fibonacci_ret=$[$fib_n_sub1 + $fib_n_sub2] } if [ $# -ne 1 ]; then echo -e "Usage:\n\t$0 (n-th fibonacci # to calculate)" exit -1 fi fibonacci $1 echo "fibonacci($1): $fibonacci_ret"