for do done 重定向,do 和done 中 有echo的时候,done后面的重定向才会打出数据
#!/bin/bash
#使用数组变量
function addarray {
local sum=0
local newarray
newarray=($(echo "$@"))
for value in ${newarray[*]}
do
sum=$[ $sum + $value ]
echo $sum
done >> test.txt
echo $sum
}
myarray=(1 2 3 4 5)
echo "The original array is:${myarray[*]}"
for value in ${myarray[*]}
do
sum=$[ $sum + $value ]
echo $sum
done >> test.txt
echo $sum
arg1=$(echo ${myarray[*]})
result=$(addarray ${myarray[*]} 2)
echo "The result is $result"
# echo "------------------------------"
# result=$(addarray $arg1)
# echo "The result is $result"