shell脚本,通过传参求斐波那契数列如(0,1,1,2,3,5,8,13..........)


[root@localhost wyb]# cat fibo.sh 
#!/bin/bash

#斐波那契数列 0,1,1,2,3,5,8,13

echo 0 > file
echo 1 >> file

count=$1

for i in `seq $count`
do
first=$(tail -2 file |head -1)
two=$(tail -1 file)
echo $((first+two)) >> file
done
cat file
[root@localhost wyb]# bash fibo.sh 10
0
1
1
2
3
5
8
13
21
34
55
89
[root@localhost wyb]# bash fibo.sh 7
0
1
1
2
3
5
8
13
21
[root@localhost wyb]# 
第二种方法,通过间接赋值来来传。用到read -p

[root@localhost wyb]# cat 2fibo.sh #!/bin/bash #斐波那契数列 0,1,1,2,3,5,8,13 echo 0 > file echo 1 >> file #count=$1 read -p "Please Input a number:" count for i in `seq $count` do first=$(tail -2 file |head -1) two=$(tail -1 file) echo $((first+two)) >> file done cat file [root@localhost wyb]# bash 2fibo.sh Please Input a number:4 0 1 1 2 3 5 [root@localhost wyb]# bash 2fibo.sh Please Input a number:6 0 1 1 2 3 5 8 13 [root@localhost wyb]# bash 2fibo.sh Please Input a number:20 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 [root@localhost wyb]#
 
 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM