#!/bin/sh
#
# 数组的声明与初始化方法
# 先声明后赋值:
declare -a array
array=(one two three)
# 声明并初始化:
array_1=(1 2 3 four)
array_2=(
1
2
3
four
)
array_3[0]=1
array_3[1]=2
array_3[2]=3
array_3[3]=four
# 数组元素的引用 ${array_name[index]}
echo "array_1[0]= ${array_1[0]}"
echo "array_3[3]= ${array_3[3]}"
echo "======================================="
# 数组的引用方法 ${array_name[*]} 或 ${array_name[@]}
echo "First Method: ${array_1[*]}"
echo "Second Method: ${array_2[*]}"
echo "Third Method: ${array_3[@]}"
echo "======================================="
# $array_name 可直接代表${array_name[0]}
echo $array
echo $array[0]
echo ${#array}
echo "======================================="
# 增加或修改元素(值)
array[4]='hello' #增加
array[1]='shells' #修改
echo ${array[*]}
echo ${array[10]} #空值
echo "======================================="
# 删除元素或数组
unset array[0]
echo ${array[*]}
unset array #删除数组
echo ${array[*]}
echo "======================================="
# 切片访问 ${array_name[*]:startindex:endindex} or ${array_name[*]:startindex:endindex} 即 array_name[startindex,endindex)
echo ${array_1[*]:1:4}
echo ${array_1[@]:1:7} #越界显示为空
echo ${array_1[*]:2} #>=2
echo ${array_1[*]::4} #<4
echo "======================================="
# 模式替换(匹配并替换) ${array_name[*]/oldchar/newchar}
array_4=(123 "231" '81o')
echo ${array_4[*]}
echo ${array_4[*]/1/replace}
echo "======================================="
# 数组的长度(元素个数)
arraylength1=${#array_1[@]}
arraylength2=${#array_2[*]}
echo $arraylength1
echo $arraylength2
echo "======================================="
# 元素的长度
elementlength3=${#array_3[3]}
elementlength3_1=${#array_3[1]}
echo $elementlength3
echo $elementlength3_1
echo "======================================="
# 数组的应用(遍历)
for (( n=0; n<${#array_1[@]}; n++ )); do
echo ${array_1[$n]}
done
echo "======================================="
for I in ${array_1[*]}; do
echo $I
done
echo "======================================="
# 特殊数组(空格作为元素的分隔符)
for I in `ls -a ~`; do
echo $I
done
for I in `head -n 10 /etc/passwd | cut -d: -f1`; do
echo "Username: $I"
done
echo "xxxxxxxxxxxx"
ARRAY=`head -n 10 /etc/passwd | cut -d: -f1`
for I in $ARRAY; do
echo "Username1: $I"
done
# 参考 http://blog.csdn.net/zhuying_linux/article/details/6778877
源码:
1 #!/bin/sh 2 # 3 4 # 数组的声明与初始化方法 5 # 先声明后赋值: 6 declare -a array 7 array=(one two three) 8 9 # 声明并初始化: 10 array_1=(1 2 3 four) 11 12 array_2=( 13 1 14 2 15 3 16 four 17 ) 18 19 array_3[0]=1 20 array_3[1]=2 21 array_3[2]=3 22 array_3[3]=four 23 24 # 数组元素的引用 ${array_name[index]} 25 echo "array_1[0]= ${array_1[0]}" 26 echo "array_3[3]= ${array_3[3]}" 27 echo "=======================================" 28 29 # 数组的引用方法 ${array_name[*]} 或 ${array_name[@]} 30 echo "First Method: ${array_1[*]}" 31 echo "Second Method: ${array_2[*]}" 32 echo "Third Method: ${array_3[@]}" 33 echo "=======================================" 34 35 # $array_name 可直接代表${array_name[0]} 36 echo $array 37 echo $array[0] 38 echo ${#array} 39 echo "=======================================" 40 41 # 增加或修改元素(值) 42 array[4]='hello' #增加 43 array[1]='shells' #修改 44 echo ${array[*]} 45 echo ${array[10]} #空值 46 echo "=======================================" 47 48 # 删除元素或数组 49 unset array[0] 50 echo ${array[*]} 51 unset array #删除数组 52 echo ${array[*]} 53 echo "=======================================" 54 55 # 切片访问 ${array_name[*]:startindex:endindex} or ${array_name[*]:startindex:endindex} 即 array_name[startindex,endindex) 56 echo ${array_1[*]:1:4} 57 echo ${array_1[@]:1:7} #越界显示为空 58 echo ${array_1[*]:2} #>=2 59 echo ${array_1[*]::4} #<4 60 61 echo "=======================================" 62 63 # 模式替换(匹配并替换) ${array_name[*]/oldchar/newchar} 64 array_4=(123 "231" '81o') 65 echo ${array_4[*]} 66 echo ${array_4[*]/1/replace} 67 echo "=======================================" 68 69 70 # 数组的长度(元素个数) 71 arraylength1=${#array_1[@]} 72 arraylength2=${#array_2[*]} 73 echo $arraylength1 74 echo $arraylength2 75 echo "=======================================" 76 77 # 元素的长度 78 elementlength3=${#array_3[3]} 79 elementlength3_1=${#array_3[1]} 80 echo $elementlength3 81 echo $elementlength3_1 82 echo "=======================================" 83 84 # 数组的应用(遍历) 85 for (( n=0; n<${#array_1[@]}; n++ )); do 86 echo ${array_1[$n]} 87 done 88 echo "=======================================" 89 90 for I in ${array_1[*]}; do 91 echo $I 92 done 93 echo "=======================================" 94 95 # 特殊数组(空格作为元素的分隔符) 96 for I in `ls -a ~`; do 97 echo $I 98 done 99 100 for I in `head -n 10 /etc/passwd | cut -d: -f1`; do 101 echo "Username: $I" 102 done 103 echo "xxxxxxxxxxxx" 104 105 ARRAY=`head -n 10 /etc/passwd | cut -d: -f1` 106 for I in $ARRAY; do 107 echo "Username1: $I" 108 done 109 110 # 参考 http://blog.csdn.net/zhuying_linux/article/details/6778877