shell脚本,按行读取文件的几种方法。



第一种方法用while实现按读取文件。
[root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 eeeeee [root@localhost wyb]# cat anhang.sh #!/bin/bash cat a.txt| while read line do echo $line sleep 1 done [root@localhost wyb]# bash anhang.sh 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 eeeeee [root@localhost wyb]# [root@localhost wyb]# cat anhang.sh #!/bin/bash while read line do echo $line sleep 1 done<a.txt [root@localhost wyb]# bash anhang.sh 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 eeeeee [root@localhost wyb]# [root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 eeeeee

#第二种方法用for循环来按行读取文件,注意:用for有一个小bug,不是一行一行来读,是按空格来分。(如果一整行没有空格,就会正常显示。) [root@localhost wyb]#
cat foranhang.sh #!/bin/bash for line in `cat a.txt` do echo $line sleep 1 done [root@localhost wyb]# bash foranhang.sh 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 eeeeee [root@localhost wyb]#

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM