Linux Shell scripting language and mathematical expressions (1)

Source: Internet
Author: User

BKJIA Translation: It feels great when you understand Shell scripts and can write them smoothly whenever needed. In this chapter, we will teach you how to use scripting to perform complex mathematical operations.

Let's start with the Fibonacci series.

The Fibonacci series, also known as the Golden split series, refers to a series of 0, 1, 1, 2, 3, 5, 8, 13, 21 ......, Each of its items is the sum of the first two items, and the first two items of the defined series are 0 and 1.

Script 1: maid. sh

#!/bin/bashecho "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

Sample output

[root@tecmint ~]# chmod 755 Fibonacci.sh[root@tecmint ~]# ./Fibonacci.shHow 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

DownloadFibonacci. sh


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.