
只有awk才支持关联数组。
shell只支持index数字类型的数组,凡是不漏悄是数字的,都会当做0,凯州或者-1,也就返孙渣是最后的那个元素。
详细参见bash的man手册。
Arrays
Bash provides one-dimensional array variables. Any variable may be used as an arraythe declare builtin
will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement
that members be indexed or assigned contiguously. Arrays are indexed using integers and are zero-based.
An array is created automatically if any variable is assigned to using the syntax name[subscript]=value.
The subscript is treated as an arithmetic expression that must evaluate to a number greater than or equal
to zero. To explicitly declare an array, use declare -a name (see SHELL BUILTIN COMMANDS below).
declare -a name[subscript] is also acceptedthe subscript is ignored. Attributes may be specified for
an array variable using the declare and readonly builtins. Each attribute applies to all members of an
array.
Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value
is of the form [subscript]=string. Only string is required. If the optional brackets and subscript are
supplied, that index is assigned to otherwise the index of the element assigned is the last index
assigned to by the statement plus one. Indexing starts at zero. This syntax is also accepted by the
declare builtin. Individual array elements may be assigned to using the name[subscript]=value syntax
introduced above.
Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid
conflicts with pathname expansion. If subscript is @ or *, the word expands to all members of name.
These subscripts differ only when the word appears within double quotes. If the word is double-quoted,
${name[*]} expands to a single word with the value of each array member separated by the first character
of the IFS special variable, and ${name[@]} expands each element of name to a separate word. When there
are no array members, ${name[@]} expands to nothing. If the double-quoted expansion occurs within a
word, the expansion of the first parameter is joined with the beginning part of the original word, and
the expansion of the last parameter is joined with the last part of the original word. This is analogous
to the expansion of the special parameters * and @ (see Special Parameters above). ${#name[subscript]}
expands to the length of ${name[subscript]}. If subscript is * or @, the expansion is the number of ele-
ments in the array. Referencing an array variable without a subscript is equivalent to referencing ele-
ment zero.
The unset builtin is used to destroy arrays. unset name[subscript] destroys the array element at index
subscript. Care must be taken to avoid unwanted side effects caused by filename generation. unset name,
where name is an array, or unset name[subscript], where subscript is * or @, removes the entire array.
The declare, local, and readonly builtins each accept a -a option to specify an array. The read builtin
accepts a -a option to assign a list of words read from the standard input to an array. The set and
declare builtins display array values in a way that allows them to be reused as assignments.
shell中数组的定义是使用 小括号来表示的,其中数组元素之间用空格作为分隔,比如:
a. 访问特定的数组元素,比如访问第2个元素,那么利用下标index=1进行访问: index支持负值,表示从后向前访问, 第一个元素的index =0, 如果从后向前,那么最后一个元素index 可以表示为index=-1.
b. 访问所有的数组元素,这时候index 用* 表示就可以了;如下:
c. 获取数组元素的个数:
在上槐明述获取所有元素的前面加上一个# 就可以了,如下:
a. 根据index 进行拿巧修改:
b. 追加元素到数组中:
当数组名称中含有shell变量的时候,此时随着shell变量的变化,那么引用的数组自然也就不同;此时可以通过如下的方式实现 变量的嵌套:
嵌套变量有两个$符号,铅敏告一个用来组合成完整的变量名称,另一个用于 标记组合后的内容是一个变量,而后一个$符号需要用 单引号括起来,剩余的部分需要用双引号括起来;然后用echo 来显示这个变量,但因为 单引号 引用的字符不会被shell解释,所以输出结果是单引号,此时用eval 来调用echo 语句就可以实现解析了;具体实例如下:
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)