Mathematical operations of Shell script arrays

Source: Internet
Author: User

Today in the shell script to write Fibonacci sequence calculation, has been constantly error, the main reason is because the shell script is too intelligent, in the array operation need to write a lot of symbols, it is easy to confuse, so a little summing up the calculation of the array elements.

First, there are several situations:

1 when assigning the result of a mathematical operation to a common variable:

(1) Let var= arithmetic expression

(2) var=$[arithmetic expression]

(3) var=$ (arithmetic expression)

(4) var=$ (expr arg1 arg2 arg3 ...)


2 when assigning the result of a mathematical operation to an array element:

(1) Let arr[$i]=arr[$[$i +1]]+arr[$[$i +2]];

(2) arr[$i]=$[arr[$[$i +1]]+arr[$[$i +2]];  /* This situation in the VIM environment, the brackets will appear red flashing, seems to be an error, in fact, not to worry about running it! */


(3) arr[$i]=$ ((arr[$[$i +1]]+arr[$[$i +2]));



Obviously, the first is the simplest way to do it. But when it comes to the calculation of array elements, it is still easy to confuse, or practice makes perfect ^_^

This article is from the "11881794" blog, please be sure to keep this source http://11891794.blog.51cto.com/11881794/1881348

Mathematical operations of Shell script arrays

Related Article

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.