linux shell里怎么用数组

linux shell里怎么用数组,第1张

split(s,a,fs) 以fs为指定分隔符将字符串s分割成一个数组a awk 'BEGIN{print split("aswd|sawa|eee",Ti,"|"),Ti[1]}'

实际上,bash中可以直接对数组赋值的,不需要使用declare。而且取长度的用法${#var}也只能是在bash中。

这些都不是符合POSIX标准的用法,而是bash所特有的。所以,要简单点的话,就将声明改为bash,即 #!/bin/bash。

当然,还有两处小错,要仔细点:

var =(1 2 3 3 5)#赋值时等号两边不能有空格

echo "${#var}#引号不成对啊,这里也可以不要引号的

命令后面的分号其实也是多余的,只要换行了,分号就不需要(当然加了也没错)

shell 并不支持关联数组。

只有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.


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7312154.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存