Shell数组例子
循环打印数组,并统计数组的个数:
[root@slavedb array]# cat a.sh
#!/bin/bash
array=(
freddy
freddie
tang
sheng
wei
)
for ((i=;i<${#array[@]};i++));do
echo "This is num $i,then content is ${array[$i]}" #$i是下标
done
echo "-----------------"
echo "array len:${#array[@]}"
使用for循环取shell数组里面的值:
#!/bin/bash
a=(aa bb cc dd ee)
for i in `seq ${#a[*]}`;do
echo -n ${a[$i]}
done
取出数组的值:
[root@bogon ~]# arr=(h0 h1 h2 h3)
[root@bogon ~]# for i in ${arr[@]}; do echo $i; done
h0
h1
h2
h3
取出数组值得下标:
[root@bogon ~]# arr=(h0 h1 h2 h3)
[root@bogon ~]# for i in ${!arr[@]}; do echo $i; done