Skip to content
Advertisement

why this code isn’t working in bash?

I am using tail -f to fetch log file and output of log this command is input to grep command with search for “ERROR” string and output of grep is used to create/write into a temporary file(tmpLog) .

command :

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

logfile content

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

Issue is , i am not getting content in tmpLog file , although it is created but with empty content But when i try this

tail -f logfile | grep -n -i “ERROR” *> tmpLog

i have content in 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

but i am getting this line a.sh:21: tail -f logfile | grep -n -i “ERROR” > tmpLog & #> /dev/null over the top of log which i don’t want

Advertisement

Answer

Do you have a file called a.sh in the working directory?

Is line 21 of this file:

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

The reason you seem to get this is because in your second example, you are effectively running:

grep -n -i "ERROR" * > tmpLog

which captures all lines in the current directory containing ERROR.

tail -f is effectively an endless stream, and as such not appropriate to redirect into a file in the way that you seem to use it.

You may want to look at this SU post about how buffering causes a problem in redirecting the output.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement