想了解shell按行读取文件的3种方法的相关内容吗,在本文为您仔细讲解shell按行读取文件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:shell,按行读取,下面大家一起来学习吧。
方法有很多,下面写出三种方法:
写法一:
#!/bin/bash
while read line
do
echo $line
done < filename(待读取的文件)
写法二:
#!/bin/bash
cat filename(待读取的文件) | while read line
do
echo $line
done
写法三:
for line in `cat filename(待读取的文件)`
do
echo $line
done
说明:
for逐行读和while逐行读是有区别的,如:
$ cat file
1111
2222
3333 4444 555
$ cat file | while read line; do echo $line; done
1111
2222
3333 4444 555
$ for line in $(<file); do echo $line; done
1111
2222
3333
4444
555