Shell使用技巧之逐行读取

重定向读取
#!/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

转载于:https://www.cnblogs.com/guge-94/p/11119807.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值