#! /bin/bash
#方法一:for循环
#for语句效率最高
file="/export/home/itms/shuju.txt"
sum=0
for i in cat $file
do
#((sum=sum+$i))
sum=expr $sum + $i
done
test -n $sum &&echo $su
#real 0m0.012s
#方法二:while循环
#while循环中执行效率最高,最常用的方法
file="/export/home/itms/shuju.txt"
sum=0
while read line
do
echo $line
((sum=sum+$line))
done<$file
echo $sum
#real 0m0.009s
#方法三:
file="/export/home/itms/shuju.txt"
exec<$file
sum=0
while read line
do
let sum=sum+$line
done
test -n $sum &&echo $sum
#real 0m0.008s
#方法四:while循环的管道方法
file="/export/home/itms/shuju.txt"
cat $file|while read line
do
echo $line
done