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 :

JavaScript

logfile content

JavaScript

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

JavaScript

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:

JavaScript

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