您的位置:首页 > 运维架构 > Shell

shell-array

2013-12-06 14:11 183 查看
shell-array

Creating Array:

$ names=("Bob" "Peter" "$USER" "Big Bad John")
$ names=([0]="Bob" [1]="Peter" [20]="$USER" [21]="Big Bad John")
# or...
$ names[0]="Bob"


You can get the number of elements of an array by using ${#array[@]}:

$ array=(a b c)
$ echo ${#array[@]}
3


There is also a second form of expanding all array elements, which is "${arrayname[*]}". This form is ONLY useful for converting arrays into a single string with all the elements joined together. The main purpose for this is outputting the array to humans:

$ names=("Bob" "Peter" "$USER" "Big Bad John")
$ echo "Today's contestants are: ${names[*]}"
Today's contestants are: Bob Peter lhunath Big Bad John


Associative Arrays

 To create an associative array, you need to declare it as such (using declare -A).

$ declare -A fullNames
$ fullNames=( ["lhunath"]="Maarten Billemont" ["greycat"]="Greg Wooledge" )
$ echo "Current user is: $USER.  Full name: ${fullNames[$USER]}."
Current user is: lhunath.  Full name: Maarten Billemont.


参考:

  1. http://hi.baidu.com/gaogaf/item/46050b15958a5225f7625c4a

  2. /article/5148459.html

  3. http://bash.cumulonim.biz/BashGuide(2f)Arrays.html

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: