第一種方法用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]#