不知道 是什么时候写的东西,整理文档时被考古发现,给那些闲着蛋疼之人,一笑而过吧。如果本文中的错误给您带来所有的精神损失,请找保险公司理陪!当然你可以告诉我 (倾诉)
数组作为一种特殊的数据结构在任何一种编程语言中都有它的一席之地,当然bash shell也不例外。本文就shell数组来做一个小的总结。
在这里只讨论一维数组的情况,关于多维数组(事实上,你得用一维数组的方法来模拟),不涉及。这里包括数组的复制,计算,删除,替换。
数组的声明:
1)array[key]=value # array[0]=one,array[1]=two
2)declare -a array # array被当作数组名
3)array=( value1 value2 value3 ... )
4)array=( [1]=one [2]=two [3]=three ... )
5)array="one two three" # echo ${array[0|@|*]},把array变量当作数组来处理,但数组元素只有字符串本身
数组的访问:
1)${array[key]} # ${array[1]}
数组的删除
1)unset array[1] # 删除数组中第一个元素
2)unset array # 删除整个数组
计算数组的长度:
1)${#array}
2)${#array[0]} #同上。 ${#array[*]} 、${#array[@]}。注意同#{array:0}的区别
数组的提取
从尾部开始提取:
array=( [0]=one [1]=two [2]=three [3]=four )
${array[@]:1} # two three four,除掉第一个元素后所有元素,那么${array[@]:0}表示所有元素
${array[@]:0:2} # one two
${array[@]:1:2} # two three
子串删除
[root@localhost dev]# echo ${array[@]:0}
one two three four
[root@localhost dev]# echo ${array[@]#t*e} # 左边开始最短的匹配:"t*e",这将匹配到"thre"
one two e four
[root@localhost dev]# echo ${array[@]##t*e} # 左边开始最长的匹配,这将匹配到"three"
[root@localhost dev]# array=( [0]=one [1]=two [2]=three [3]=four )
[root@localhost dev]# echo ${array[@] %o} # 从字符串的结尾开始最短的匹配
one tw three four
[root@localhost dev]# echo ${array[@] %%o} # 从字符串的结尾开始最长的匹配
one tw three four
子串替换
[root@localhost dev]# array=( [0]=one [1]=two [2]=three [3]=four )
第一个匹配到的,会被删除
[root@localhost dev]# echo ${array[@] /o/m}
mne twm three fmur
所有匹配到的,都会被删除
[root@localhost dev]# echo ${array[@] //o/m}
mne twm three fmur
没有指定替换子串,则删除匹配到的子符
[root@localhost dev]# echo ${array[@] //o/}
ne tw three fur
替换字符串前端子串
[root@localhost dev]# echo ${array[@] /#o/k}
kne two three four
替换字符串后端子串
[root@localhost dev]# echo ${array[@] /%o/k}
one twk three four
#!/bin/bash
area[11]=23
area[13]=37
area[51]=UFOs
# Array members need not be consecutive or contiguous.
# Some members of the array can be left uninitialized.
# Gaps in the array are okay.
# In fact, arrays with sparse data ("sparse arrays")
#+ are useful in spreadsheet-processing software.
echo -n "area[11] = "
echo ${area[11]} # {curly brackets} needed.
echo -n "area[13] = "
echo ${area[13]}
echo "Contents of area[51] are ${area[51]}."
# Contents of uninitialized array variable print blank (null variable).
echo -n "area[43] = "
echo ${area[43]}
echo "(area[43] unassigned)"
echo
# Sum of two array variables assigned to third
area[5]=`expr ${area[11]} + ${area[13]}`
echo "area[5] = area[11] + area[13]"
echo -n "area[5] = "
echo ${area[5]}
area[6]=`expr ${area[11]} + ${area[51]}`
echo "area[6] = area[11] + area[51]"
echo -n "area[6] = "
echo ${area[6]}
# This fails because adding an integer to a string is not permitted.
echo; echo; echo
# -----------------------------------------------------------------
# Another array, "area2".
# Another way of assigning array variables...
# array_name=( XXX YYY ZZZ ... )
area2=( zero one two three four )
echo -n "area2[0] = "
echo ${area2[0]}
# Aha, zero-based indexing (first element of array is [0], not [1]).
echo -n "area2[1] = "
echo ${area2[1]} # [1] is second element of array.
# -----------------------------------------------------------------
echo; echo; echo
# -----------------------------------------------
# Yet another array, "area3".
# Yet another way of assigning array variables...
# array_name=([xx]=XXX [yy]=YYY ...)
area3=([17]=seventeen [24]=twenty-four)
echo -n "area3[17] = "
echo ${area3[17]}
echo -n "area3[24] = "
echo ${area3[24]}
# -----------------------------------------------
exit 0
[root@localhost shell]# ./ex66.sh
area[11] = 23
area[13] = 37
Contents of area[51] are UFOs.
area[43] =
(area[43] unassigned)
area[5] = area[11] + area[13]
area[5] = 60
expr: 非数值参数
area[6] = area[11] + area[51]
area[6] =
area2[0] = zero
area2[1] = one
area3[17] = seventeen
area3[24] = twe