From: http://blog.csdn.net/snrqtdhuqf/article/details/7242309
In shell, there are two ways to assign values to array variables:
(1) name = (value1... valuen) The subscript starts from 0.
(2) name [Index] = Value
Example:
- #! /Bin/sh
- # Arraytest
- Name = (yunix yhx YFJ)
- Echo "array is :$ {name [@]}"
- Echo "array length is :$ {# name [*]}"
- Echo $ {name [1]}
- Name [1] = Yang
- Echo $ {name [1]}
- Read-a name
- Echo $ {name [1]}
- Echo "loop the array"
- Len = $ {# name [*]}
- I = 0
- While [$ I-lt $ Len]
- Do
- Echo $ {name [$ I]}
- Let I ++
- Done
Result:
Array is: yunix yhx YFJ
Array length is: 3
Yhx
Yang
A B C D E
B
Loop the Array
A
B
C
D
E
The following is an array output example.
Example:
#! /Bin/sh
-
- # Arrayloopout
-
- Read-A Array
-
- Len =$ {# array [*]}
-
- Echo "array's length is $ len"
-
- Echo "use while out the array :"
-
- I = 0
-
- While [$ I-lt $ Len]
-
- Do
-
- Echo-n "$ {array [$ I]}"
- Let I ++
-
- Done
-
- Echo
-
- Echo "use for out the array :"
-
- For (j = 0; j <"$ len"; j = J + 1 ))
-
- Do
-
- Echo-N $ {array [$ J]}
-
- Done
-
- Echo
-
- Echo "use for in out the array :"
-
- For value in $ {array [*]}
-
- Do
-
- Echo-N $ Value
-
- Done
Result:
a B C D E F G
array's length is 7
use while out the array:
abcdefg
use for out the array:
abcdefg
use for in out the array:
abcdefg