一、使用for循环
for line in `cat $filename`
do
echo $line
done
或者
for line in $(cat $filename)
do
echo $line
done
二、使用while循环
while read -r line
do
echo $line
done < $filename
While循环中read命令从标准输入中读取一行,并将内容保存到变量line中。在这里,-r选项保证读入的内容是原始的内容,意味着反斜杠转义的行为不会发生。输入重定向操作符< file打开并读取文件file,然后将它作为read命令的标准输入
或者
cat $filename | while read LINE
do
echo $LINE
done
或者
cat datafile.txt | while myline=$(line)
do
echo "LINE:"$myline
done
while myline=$(line)
do
echo "LINE:"$myline
done < datafile.txt
或者使用read命令读取变量数据
cat datafile.txt | while read paraa parab parac
do
echo "PARAA:"$paraa
echo "PARAB:"$parab
echo "PARAC:"$parac
done
或
while read paraa parab parac
do
echo "PARAA:"$paraa
echo "PARAB:"$parab
echo "PARAC:"$parac
done < datafile.txt