欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

shell:读取文件的每一行内容并输出 有大用

shell:读取文件的每一行内容并输出

写法一:
----------------------------------------------------------------------------
#!/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
 
#echo ${str##*fo}
#echo ${str#fo}
while read line
do
   wget -p ${line:0:59}
done < '/root/mysql/mysql.log';
  

来自 https://www.cnblogs.com/iloveyoucc/archive/2012/07/10/2585529.html

普通分类: