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]} [[email protected] ~]# bash arry.sh xiaoming xiaoli dalong sanlang [[email protected] ~]#
单独赋值 [[email protected] ~]# vim arry02.sh name[0]="xiaoming" name[2]="xiaoli" name[3]="lusa" echo ${name[0]} echo ${name[1]} echo ${name[2]} ~ [[email protected] ~]# bash arry02.sh xiaoming xiaoli [[email protected] ~]# name[0]="xiaoming" [[email protected] ~]# echo ${name[0]} xiaoming [[email protected] ~]#
2.获取字符串的长度
[[email protected] ~]# name="oldboy" [[email protected] ~]# echo ${#name} 6 [[email protected] ~]#
3.获取数组的长度
[[email protected] ~]# 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[@]} [[email protected] ~]# [[email protected] ~]# bash arry02.sh xiaoming xiaoli 3 3
for循环取值
[[email protected] ~]# 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 [[email protected] ~]# [[email protected] ~]# 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)) [[email protected] ~]# cat a.sh #!/bin/bash arry=($(ls)) for i in ${arry[*]} do echo $i done [[email protected] ~]#
arry取值的反思:
1.直接去arry ${arry[*]}
2.for循环获取长度,按照索引输出数组的值
时间: 2024-10-26 07:52:04