#数组元素遍历 foreach 操作
#参数:1数组;
2 回调函数(用户定义); 3用户数据参数(可选)
#回调函数:对数组每个元素调用该函数
#回调函数参数:参数:1元素在数组中的下标(从
0 开始);2
元素; 3用户数据参数(可选)
#例子:
# 对数组 xrsh_array 中每个元素调用函数 xrsh_fn
# 对数组 xrsh_array 中每个元素调用函数 xrsh_fn
# xrsh_fn()
# {
# echo index $1, item $2, usr arg $3
# }
# xrsh_array=(i1,i2,i3)
# xrsh_tmp=`echo ${xrsh_array[*]}`
# xrsh_foreach "$xrsh_tmp" xrsh_fn
#注意:数组作为参数使用时需要先转换
function xrsh_foreach()
{
local _xrsh_tmp
local _xrsh_cnt=0
local _xrsh_array=`echo"$1"`
for _xrsh_tmp
in ${_xrsh_array[*]};
do
$2 $_xrsh_cnt $_xrsh_tmp $3
_xrsh_cnt=$(( $_xrsh_cnt
+ 1 ))
done
}