脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - shell - shell按行读取文件的3种方法

shell按行读取文件的3种方法

2023-02-07 11:53shell教程网 shell

这篇文章主要介绍了shell按行读取文件的3种方法,需要的朋友可以参考下

方法有很多,下面写出三种方法:
写法一:

复制代码 代码如下:

#!/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

延伸 · 阅读

精彩推荐