歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Linux Shell腳本語言與數學表達式

Linux Shell腳本語言與數學表達式

日期:2017/3/1 9:52:50   编辑:SHELL編程

當你理解了Shell腳本,每當需要時都能流暢編寫時,那種感覺很爽的。本章中,我們將教你用腳本語言進行比較復雜的數學運算。

讓我們從斐波那契數列開始吧。

斐波那契數列,又稱黃金分割數列,指的是這樣一個數列:0、1、1、2、3、5、8、13、21……,它的每一項都是前兩項的和,定義數列的首兩項為0、1。

腳本1:Fibonacci.sh

#!/bin/bash
echo "How many numbers do you want of Fibonacci series ?"
read total
x=0
y=1
i=2
echo "Fibonacci Series up to $total terms :: "
echo "$x"
echo "$y"
while [ $i -lt $total ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done示例輸出

[root@tecmint ~]# chmod 755 Fibonacci.sh
[root@tecmint ~]# ./Fibonacci.sh

How many numbers do you want of Fibonacci series ?
10
Fibonacci Series up to 10 terms ::
0
1
1
2
3
5
8
13
21
34

下載Fibonacci.sh

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2013年資料/9月/27日/Linux Shell腳本語言與數學表達式

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

Copyright © Linux教程網 All Rights Reserved