加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 在bash中读取文件的前三行

发布时间:2020-12-14 01:38:08 所属栏目:Linux 来源:网络整理
导读:我有以下 shell脚本来读取文件的前三行并将它们打印到屏幕 – 它无法正常工作,因为它打印出2,3,4行而不是行1,2,3 – 我在做什么错了? exec 6 rhyme.txtwhile read file 6 ;do read line1 6 read line2 6 read line3 6 echo $line1 echo $line2 echo $line3
我有以下 shell脚本来读取文件的前三行并将它们打印到屏幕 – 它无法正常工作,因为它打印出2,3,4行而不是行1,2,3 – 我在做什么错了?

exec 6< rhyme.txt

while read file <&6 ;
do
        read line1 <&6
        read line2 <&6
        read line3 <&6

        echo $line1 
        echo $line2 
        echo $line3 
done

exec 6<&-

感谢您的回答 – 我知道head命令但是想要使用读取和文件描述符来显示前三行

解决方法

您还可以组合head和while命令:

head -3 rhyme.txt | 
while read a; do
  echo $a; 
done

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读