shell脚本计算斐波那契数列

计算斐波那契数列 【1,1,2,3,5,8,,,,,】

#!/bin/bash
 
n=$1
num=(1 1)
i=2
while [[ $i -lt $n ]]
do
    let num[$i]=num[$i-2]+num[$i-1]
    let i++
done
echo ${num[$n-1]}

计算结果:

# bash -x bb.sh 4  //分步执行结果
+ n=4
+ num=(1 1)
+ i=2
+ [[ 2 -lt 4 ]]
+ let 'num[2]=num[2-2]+num[2-1]'
+ let i++
+ [[ 3 -lt 4 ]]
+ let 'num[3]=num[3-2]+num[3-1]'
+ let i++
+ [[ 4 -lt 4 ]]
+ echo 3
3


#

# bash bb.sh 4 //直接执行结果
3


原文地址:https://www.cnblogs.com/carriezhangyan/p/9546263.html