#! /bin/bash # # fibonacci - Shell script for printing out Fibonacci Numbers # #--------------------------------------------------------------------------------- # # The Fibonacci sequence is 1, 1, 2, 3, 5, 8, 13, 21, ... # # Defined by: # Fib(1) = 1 , Fib(2) = 1 , Fib(N) = Fib(N-1) + Fib(N-2) for all N >= 3. # # #--------------------------------------------------------------------------------- # # Written by Todd Lisonbee # # Version: 0.1 # # This is a mostly useless script that I wrote to help me learn Bash Scripting. # On my computer overflow occurs after the 46th Fibonacci Number. # # usage="Usage: fibonacci [-v] [number of fibonacci numbers to print]" if [ -z "$1" ]; then echo "$usage" exit 1 elif [ -n "$(echo $1 | grep '^-')" ]; then while getopts "vh" opt; do case $opt in v ) echo "Fibonacci script version 0.1" echo "Written by Todd Lisonbee" ;; h ) echo "Here is some help" echo "$usage" ;; \? ) echo "$usage" exit 1 ;; esac done else first=1 second=1 count=2 if (( $1 >=1 )); then echo $first else return fi if (( $1 >= 2 )); then echo $second else return fi while [ $count -lt $1 ]; do current=$(($first+$second)) echo $current first=$second second=$current count=$(($count+1)) done fi