Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

b) Script2.sh: Write a shell script that prints out the numbers of the Fibonacci

ID: 3817982 • Letter: B

Question

b) Script2.sh: Write a shell script that prints out the numbers of the Fibonacci series. This series of numbers is one where every number is the sum of the previous two numbers before it. For example, the first 10 number of the series is {0, 1, 1, 2, 3, 5, 8, 13, 21, 34}. The series is infinite, so assume that one command line argument is given to your script which is the total number of Fibonacci numbers to display. If a '1' is given, have it print off just the first number. If a '2' is given, have it just print off the first two numbers. If a '3' or bigger is given, have it print off the first two numbers, then have your code calculate the rest automatically. Assume that a logical number will always be given to you (don't worry about checking to see if the number is bigger than 0, for example). As an example, if you run your script using the command ./script2.sh 5, your script should display to the screen the following: 0 1 1 2 3

Explanation / Answer

if [ $# -eq 1 ]
then
    n=$1
else
    echo -n "Enter a Number :"
    read n
fi

f1=0
f2=1

echo "The Fibonacci sequence for the number $n is : "

for (( i=0;i<=n;i++ ))
do
     echo -n "$f1 "
     fn=$((f1+f2))
     f1=$f2
     f2=$fn
done

echo