首页 > 系统服务 > 详细

Shell使用技巧之逐行读取

时间:2019-07-02 14:38:37      阅读:90      评论:0      收藏:0      [点我收藏+]

重定向读取

#!/bin/bash
while read line
 do
   echo $line
 done < /etc/passwd 

 

管道读取

#!/bin/bash
cat /etc/passwd | while read line
do
  echo $line
done

 

文件描述符

#!/bin/bash
exec 3<"/etc/passwd"
while read line <&3
 do
  echo $line
 done

 

for循环

#!/bin/bash
IFS=$\n
for file in `cat /etc/passwd`
 do
  echo $file
 done

# PS:这里的IFS格式设置不要错了,必须这么写IFS才能生效,其他方式都不对;

从执行速度上for最快、其次重定向、接着是文件描述符、最后是管道

参考文档:https://blog.csdn.net/apache0554/article/details/47006609

Shell使用技巧之逐行读取

原文:https://www.cnblogs.com/guge-94/p/11119807.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!