#!/bin/bash
function addarray
{
local sum=0
local array
array=(`echo $@`)
echo "array in function adday: ${array[*]}"
for((i=0 ; i<5 ; i++))
do
echo "i=$i"
echo "\$array[$i]=${array[$i]}"
sun=$[ $sum + ${array[$i]} ]
done
echo $sum
}
myarray=(1 2 3 4 5)
echo "The original array is ${myarray[*]}"
arg=`echo ${array[*]}`
sum=`addarray $arg`
echo "sum=$sum"
执行后for循环内sun=$[ $sun + ${array[i]} ] 行出现语法错误,
函数内 echo "\$array[$i]=${array[$i]}" 无法显示苏族元素的实际值
求大虾帮忙看看,在命令行里echo ${array[2]} 是可以显示元素值的啊?
梦里花落0921
回首忆惘然