方法1:while循环中
while read line
do
echo $line
done < filename
方法2:管道法: cat $filename | while read line
cat filename | while read line
do
echo $line
done
方法3:for循环
for line in `cat filename`
do
echo ${line}
done
示例:加入对于文件的判断
#!/bin/bash
if [ $# -ne 1 ];then
echo "Usage:$0 filename"
exit 1
fi
file=$1
if [ ! -f $file ];then
echo "the $file is not a file"
exit 2
fi
count=0
while read line
do
let count++
echo "第${count}行: ${line}"
done < $file
echo -e "\ntotle $count lines read"
exit 0