while循环学习之统计流量
/application/apache/logs/bbs-access_log日志文件中任意一行的格式如下,以空格为间隔第十列(2632)为此次请求内容的字节数大小
192.168.220.1 - - [13/Oct/2015:21:55:16 +0800] "GET /install/images/stepnum.gif HTTP/1.1" 200 2632
每一行就是一次请求
需求:将每一行的该字段的值加起来的总和就是总流量
关键词:while read line
#!/bin/sh i=0 sum=0 while read line do i=$(echo $line|awk '{print $10}') if expr $i + 0 &>/dev/null;then ((sum=sum+$i)) fi done </application/apache/logs/bbs-access_log echo $sum [root@lamp scripts]#
按行读取文件的方式:
方法一:
cat filename|while read line
do
............
done
方法二;
while read line
do
.............
done <filename
方法三:
exec < filename
while read line
do
.............
done