寫法一:
----------------------------------------------------------------------------
#!/bin/bash
while read line
do
echo $line
done < file(待讀取的檔案)
----------------------------------------------------------------------------
寫法二:
----------------------------------------------------------------------------
#!/bin/bash
cat file(待讀取的檔案) | while read line
do
echo $line
done
----------------------------------------------------------------------------
寫法三:
----------------------------------------------------------------------------
for line in `cat file(待讀取的檔案)`
do
echo $line
done
----------------------------------------------------------------------------
說明:
for逐行讀和while逐行讀是有區別的,如:
$ cat file
aaaa
bbbb
cccc dddd
$ cat file | while read line; do echo $line; done
aaaa
bbbb
cccc dddd
$ for line in $(<file); do echo $line; done
aaaa
bbbb
cccc
dddd
== 實踐 ===
#! bin/sh
while read line
do
wget -p ${line:0:59}
done < '/root/mysql/mysql.log'
轉自:http://blog.sina.com.cn/s/blog_67b7d7e40101b723.html