首页 > 解决方案 > How to handle negative arguements in a fibonacci series using bash script in linux

问题描述

How can I add a condition that will deal with a negative number input for my Fibonacci series in bash script? This is my code:

clear
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then 
  echo "Usage: Program that finds the fibonacci series"
  echo "Options:"
  echo "-h   shows this help information " 
  echo "-lt  the value on the left is less than the value on the right"
  echo "expr  evaluates a given expression and displays its corresponding output "
  exit 1

fi

echo 'This program will find the Fibonnacci Series'
echo "Enter the number of terms you want to be generated"
read n
f1=0
f2=1
i=2
echo "The following is the Fibonacci Series upto $n term:"
echo $f1
echo $f2
while [ $i -lt $n ]
do
  i=`expr $i + 1`
  f3=`expr $f1 + $f2
  echo $f3
  f1=$f2
  f2=$f3
done

标签: bashfibonaccinegative-number

解决方案


推荐阅读