指定n数值,求1+…+n和
[root@server opt]# more n.sh
#!/bin/sh
sum=0
function ff_forsum(){
for num in $(seq $1)
do
sum=$(($sum+$num))
done
}
function ff_whilesum(){
i=1
while(($i<=$1))
do
sum=$(($sum+$i))
i=$(($i+1))
done
}
# call ff_forsum
ff_forsum $1
echo $sum
sum=0
# call ff_whilesum
ff_whilesum $1
echo $sum