This article is intended to review the definition and assignment of arrays in shell script programming, to read arrays, and to get the length of an array
Knowledge Reserve
Array : Multiple contiguous separate memory spaces, each memory space equivalent to one variable
logically, the array we see is " of continuous ", but mapping to real physical memory space is" the Messy "
Bash supports one-dimensional arrays (which do not support multidimensional arrays) and does not limit the size of arrays.
Array 2 elements : Array name [index], according to the different index can be divided into the following 2 kinds of
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/95/wKiom1YBVVah4QUcAAZPQEXvGiY088.bmp "title=" A.bmp " alt= "Wkiom1ybvvah4qucaazpqexvgiy088.bmp"/>
Array Definition and Assignment
In the shell, the array is represented by parentheses, and the elements of the array are separated by a "space" symbol.
The general form of the definition array is:
1 |
|
Array_name= ( value1 value2 ... valuen) |
How to assign a value
(1) One-time assignment of an element
(2) Copy all elements at once
1 |
|
array= ( "Val1"   "Val2"   ...) |
" Span style= "line-height:0px;" > (3) Specify the index to assign the value
1
|
|
array= ([0]="Val1" [3]="Val2") |
(4) Assigning values from user interactive input
Read array [reference array]
the general format for reading the element values for an array-specified index is:
You can also read all the elements in the array at once
1 2
|
|
${array[*]} ${array[@]} |
Example 1 : Enter 5 numbers, save the array, and reference the array to reverse the output of these 5 numbers
Code implementation
1 2 3 4 5 6 7 8 9 10 11
|
|
#!/bin/bash forIinch$ (Seq0 4) Do Read- P"Enter the 5 nums:"num numarray[$i]= $num Done
forQinch$ (Seq4 -1 0) Do echo ${numarray[$q]} Done |
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/95/wKiom1YBXBHSevIZAAkYIF5yCS8247.bmp "title=" A.bmp " alt= "Wkiom1ybxbhsevizaakyif5ycs8247.bmp"/>
Get array length
Gets the array length into the number of array elements, and also gets the length of the specified index element value
Gets the number of array elements in the general format:
1 2 3 |
|
Length=${# array[@ ]} Span style= "color: #008000;" ># or length=${# array[< Span style= "Color:rgb (255,0,0);" >*]} |
Example 1 : Please note that you must not omit it, see the following example
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/96/wKiom1YBXrKT_d-JAASIAAGHOA0752.bmp "title=" A.bmp " alt= "Wkiom1ybxrkt_d-jaasiaaghoa0752.bmp"/>
Please note : When the # number is omitted, it becomes the copy of all elements of the array to the variable length, if you want to print the number, remember #
Gets the length of the element value for the specified number of indexes, in the general format:
1
|
|
length=${#array[index]} |
Example 2 : Gets the length value of the second element color in the above example
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/96/wKiom1YBX9yTu6v4AAKTYL4vs6A888.bmp "title=" A.bmp " alt= "Wkiom1ybx9ytu6v4aaktyl4vs6a888.bmp"/>
This article is from the "Blue Warehouse" blog, be sure to keep this source http://bluebox.blog.51cto.com/8852456/1697256
[9-13] Shell Series 8--Arrays