1.复制和取值
一起赋值 #!/bin/bash arry_name=(xiaoming xiaoli dalong sanlang) echo ${arry_name[0]} echo ${arry_name[1]} echo ${arry_name[2]} echo ${arry_name[3]} [root@oldboy ~]# bash arry.sh xiaoming xiaoli dalong sanlang [root@oldboy ~]#
单独赋值 [root@oldboy ~]# vim arry02.sh name[0]="xiaoming" name[2]="xiaoli" name[3]="lusa" echo ${name[0]} echo ${name[1]} echo ${name[2]} ~ [root@oldboy ~]# bash arry02.sh xiaoming xiaoli [root@oldboy ~]# name[0]="xiaoming" [root@oldboy ~]# echo ${name[0]} xiaoming [root@oldboy ~]#
2.获取字符串的长度
[root@oldboy ~]# name="oldboy" [root@oldboy ~]# echo ${#name} 6 [root@oldboy ~]#
3.获取数组的长度
[root@oldboy ~]# cat arry02.sh name[0]="xiaoming" name[2]="xiaoli" name[3]="lusa" echo ${name[0]} echo ${name[1]} echo ${name[2]} echo ${#name[*]} echo ${#name[@]} [root@oldboy ~]# [root@oldboy ~]# bash arry02.sh xiaoming xiaoli 3 3
for循环取值
[root@oldboy ~]# cat arr.sh arry=( 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 ) for i in ${arry[*]} do echo $i done [root@oldboy ~]# [root@oldboy ~]# bash arr.sh 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4
#!/bin/bash arry=( 10.0.0.0 10.0.0.1 10.0.0.2 ) for((i=0;i<${#arry[*]};i++)) do echo ${arry[$i]} done ~
取命令的值
命令结果放在数组里面: arry=($(ls)) [root@oldboy ~]# cat a.sh #!/bin/bash arry=($(ls)) for i in ${arry[*]} do echo $i done [root@oldboy ~]#
arry取值的反思:
1.直接去arry ${arry[*]}
2.for循环获取长度,按照索引输出数组的值
本文出自 “砖家博客” 博客,请务必保留此出处http://wsxxsl.blog.51cto.com/9085838/1825932
原文地址:http://wsxxsl.blog.51cto.com/9085838/1825932