zoukankan      html  css  js  c++  java
  • 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]#
    
    
    
     
  • 相关阅读:
    laravel5.2总结--blade模板
    laravel5.2总结--响应
    laravel5.2总结--请求
    git总结
    laravel5.2总结--路由
    Get与Post的一些总结
    python库安装
    iptables的recent模块
    iptables
    dmucs与distcc
  • 原文地址:https://www.cnblogs.com/wangyuebo/p/5817730.html
Copyright © 2011-2022 走看看