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