为什么这段代码不适用于bash?

时间:2017-04-05 03:53:01

标签: linux bash shell

我正在使用tail -f来获取日志文件并输出log这个命令输入到grep命令并搜索" ERROR" grep的字符串和输出用于创建/写入临时文件( tmpLog )。

命令:

tail -f logfile |  grep -n -i "ERROR" > tmpLog

日志文件内容

100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.003
100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.0033
100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.004
100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.005
100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.006
100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.007
100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.0023
100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.0043
100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.004
100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.005
100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.006
100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.007
100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.0043

问题是,我没有在tmpLog文件中获取内容,尽管它已创建但内容为空 但是当我尝试这个时

tail -f logfile | grep -n -i" ERROR" * GT; tmplog之外

我在tmpLog中有内容

a.sh:21:    tail -f logfile |  grep -n -i "ERROR" > tmpLog &  #> /dev/null
logfile:1:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.003
logfile:2:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.0033
logfile:3:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.004
logfile:4:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.005
logfile:5:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.006
logfile:6:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.007
logfile:7:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.0023
logfile:8:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.0043
logfile:9:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.004
logfile:10:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.005
logfile:11:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.006
logfile:12:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.007
logfile:13:100.66.230.2 - - [03/Apr/2017:11:40:23 +0900] "ERROR /favicon.ico HTTP/1.1" 200 26238 0.0043

但我得到的这行 a.sh:21:tail -f logfile | grep -n -i" ERROR" > tmpLog& #> / dev / null 在我不想要的日志顶部

1 个答案:

答案 0 :(得分:1)

您是否在工作目录中有一个名为a.sh的文件?

此文件的第21行:

  

tail -f logfile | grep -n -i" ERROR" > tmpLog& #>的/ dev / null的

你似乎得到这个的原因是因为在你的第二个例子中,你正在有效地运行:

grep -n -i "ERROR" * > tmpLog

捕获当前目录中包含 ERROR 的所有行。

tail -f实际上是一个无休止的流,因此不适合以您似乎使用它的方式重定向到文件中。

您可能需要查看this SU post有关缓冲在重定向输出时如何导致问题的信息。