写法一:web
----------------------------------------------------------------------------bash
#!/bin/bashorm
while read lineit
dofile
echo $linewebkit
done < filename(待读取的文件)脚本
----------------------------------------------------------------------------word
写法二:tab
----------------------------------------------------------------------------文件
#!/bin/bash
cat filename(待读取的文件) | while read line
do
echo $line
done
----------------------------------------------------------------------------
写法三:
----------------------------------------------------------------------------
for line in `cat filename(待读取的文件)`
do
echo $line
done
----------------------------------------------------------------------------
说明:
for逐行读和while逐行读是有区别的,如:
$ cat file
1111
2222
3333 4444 555
$ cat file | while read line; do echo $line; done
1111
2222
3333 4444 555
$ for line in $(<FILE); style="WORD-WRAP: break-word" p="" done<="" echo="" do="">
1111
2222
3333
4444
555
额外注意的地方
1.若是文件中是经过tab分隔的,覆盖追加到其余文件时变成了以空格为分隔符。
这个暂时尚未搞清楚什么缘由。
2.执行脚本
#!/bin/bash
#if [ $# -eq 0 ];then
# echo "执行该脚本须要文件参数,请输入参数"
# exit 1
#fi
file=/home/liukai/backup/learningShell/a.txt
echo $file
echo "第一种方式"
while read line
do
echo $line
done < $file
echo "第二种方式"
cat $file | while read line
do
echo $line
done
echo "第三种方式"
for f in `cat $file`
do
echo $f
done
执行结果
/home/liukai/backup/learningShell/a.txt第一种方式jack 20 198 165joe 25 176 140lucy 17 165 100第二种方式jack 20 198 165joe 25 176 140lucy 17 165 100第三种方式jack20198165joe25176140lucy17165100