shell读取文件的方式有两种,while循环读和for循环读
while循环读方式
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
cat filename | while read line
do
etho $line
done
for循环读方式
#/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
for line in `cat filename(待读取的文件)`
do
echo $line
done
这两种方式的另外的写法
#/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
while read line
do
echo $line
done < filename
while和for对文件的读取是有区别的
while对文件读是逐行读完后跳转到下行
for对文件的读是按字符串的方式进行的,遇到空格什么后,再读取的数据就会换行显示
while相对for的读取很好的还原数据原始性
matthew@ubuntu:~/workspace/shell$ cat 011.txt
1111 2222 3333 aaaaa
bbbbbbbbbbbbbbbbbbbb
ccccccccc ddddddddd
matthew@ubuntu:~/workspace/shell$
matthew@ubuntu:~/workspace/shell$ ./for.sh
1111
2222
3333
aaaaa
bbbbbbbbbbbbbbbbbbbb
ccccccccc
ddddddddd
matthew@ubuntu:~/workspace/shell$
matthew@ubuntu:~/workspace/shell$ ./while.sh
1111 2222 3333 aaaaa
bbbbbbbbbbbbbbbbbbbb
ccccccccc ddddddddd
matthew@ubuntu:~/workspace/shell$